Question

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

Was it helpful?

Solution

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.

OTHER TIPS

I had a similar issue using Paramiko. I was concatenating strings to create my paths

localpath = root + '\' + filename
remotepath = root + '\' + filename

I found that the strings worked but the concatenated strings did not. Sooooooo

local_path = str(localpath)
remote_path = str(remotepath)
sftp.get(remote_path,local_path)

It may seem ugly, but it worked for me.

I'd better use for such strings raw string literals. Like

remotePath = r"/etc/conf/file-cfg.xml"

or, which is better, use path-related functions to create platform-independent paths.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top