I'm writing a Python script that needs to download a remote xml file to parse it.
I'm using paramiko for it.
Here is the script:
def copyFile(ip, user, pwd, remotePath, localPath):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(ip,username=user,password=pwd)
### Copy remote file to server
sftp = ssh.open_sftp()
sftp.get(remotePath,localPath)
sftp.close()
ssh.close()
return ["OK",0,0]
except IOError as e:
flash(str(e)+" IOERROR")
return ["IOERROR: " + str(e),0,0]
except Exception as e:
flash(str(e)+" OTHER EXCEPTION")
return ["Error: " + str(e),0,0]
The code returns and IOError saying that the file don't exists.
But If I swap the variable for strings, it works perfectly.:
`sftp.get("/etc/conf/file-cfg.xml","./conf/file-cfg.xml")`
The variables are being passed correctly:
`copyFile(ip,username,pwd,"/etc/conf/"+i,"."+i)`
I lost a lot of time trying to figure out whats is wrong withou any success.
My remote machine : Ubuntu 13.10 x64 Local Machine: Windows 7 x64
A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.
SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels. A typical use case is: client = SSHClient() client.
Connect : Connect to sftp using correct credentials. Upload: Upload file in specific remote path. Download : Download the file if it exists. Wait time is in seconds and we can retry as per requirement.
Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model.
I ran across the same issue, but later figured out there was a trailing \n character, which is unseen.
So I'd suggest to call strip() method to remove invisible leading/trailing characters, which could be the offending ones
strip() worked at my side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With