1

Good afternoon,

I am getting the following error whenever I am trying to copy a test file from one directory to another on a remote server:

Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\site-packages\paramiko-1.12.0-py2.7.egg\paramiko\sftp_client.py", line 612, in put file_size = os.stat(localpath).st_size WindowsError: [Error 3] The system cannot find the path specified: '/brass/prod/bin/chris/test1/km_cust'

The file I am looking to copy is called km_cust.

I am executing these commands in python 2.7.

Please note that the hostname, uid, and password were changed to generic versions and the real hostname, uid, and password can be used to ssh to the box in question and preform all functionality.

Here is my code:

import paramiko

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect('hostname',username='test',password='pw')
filepath = '/brass/prod/bin/chris/test1/km_cust'
localpath = 'brass/prod/bin/chris/test2'

sftp = s.open_sftp()
sftp.put(filepath, localpath)

Any help will be apprecaited. Let me know if any other information is needed.

Chris
  • 569
  • 2
  • 8
  • 15
  • Is there actually a file named `/brass/prod/bin/chris/test1/km_cust` on the system you're connecting to? (Note that if user `test` lives in a chroot jail, you need to check this as `test`, not as `root` or your normal login.) – abarnert Nov 04 '13 at 20:59
  • Meanwhile, you say you're looking to copy a file named `test_cust`, but your error message is about a file named `km_cust`. Are you sure this is your actual code? This could be one of those stupid bugs where you fixed the path in your text editor, but didn't save the file, and ran the old version of the script… – abarnert Nov 04 '13 at 21:00
  • I apologize and thank you for the quick comments. The file I was testing with was called km_cust, I had changed the name in this example by mistake. Also note that hostname, username, and password are changed but I am currently able to ssh on to the box with the hostname, uid, and password. I adjusted my example above as well. Thank you again for any help. – Chris Nov 04 '13 at 21:06
  • You still haven't answered the most important question: When you ssh onto the box with that hostname, uid, and password, does the file specified by `filepath` actually exist? – abarnert Nov 04 '13 at 21:11
  • That would be important, wouldn't it. :) Yes it does. The file is located in the directory- hostname:/brass/prod/bin/chris/test1/km_cust – Chris Nov 04 '13 at 21:23

1 Answers1

1

The problem is that put copies a local file—that is, a file n your Windows box—to the server. As the documentation says:

put(self, localpath, remotepath, callback=None, confirm=True)
Copy a local file (localpath) to the SFTP server as remotepath.

Note that you're also specifying (or at least naming) the paths backward… but that doesn't really matter here, because neither one is actually a local path. So when you do this:

sftp.put(filepath, localpath)

… it's looking for a file named '/brass/prod/bin/chris/test1/km_cust' on your Windows box, and of course it can't find such a file.

If you want to copy a remote file to a different remote file, you need to do something like this:

f = sftp.open(filepath)
sftp.putfo(f, localpath)

Or:

f = sftp.open(localpath, 'wx')
sftp.getfo(filepath, f)

Also, I'm guessing your filepath is supposed to start with a /.


However, this probably isn't what you wanted to do in the first place. Copying a file from the remote server to the remote server via sftp involves downloading all of the bytes to your Windows machine and then uploading them back to the remote machine. A better solution would be to just tell the machine to do the copy itself:

s.exec_command("cp '{}' '{}'".format(filepath, localfile))
s.close()

Note that in anything but the most trivial of cases, you're going to have to deal with the Channel and its in/out/err and wait on its exit status. But I believe that for this case, you should be fine.

abarnert
  • 354,177
  • 51
  • 601
  • 671