Python - file transfer between two remote servers, python script exception

I am trying to copy a file between two servers from localServer

, say from server-A

to server-B

. I am using the paramiko package in python.

So, there are three servers, namely localServer

, server-A

and server-B

. Please see the code below, it is explanatory and please let me know where I am going wrong.

The algorithm I'm using:

  • I am trying to run a paramiko_test.py

    file from localServer

    .
  • paramiko_test.py

    executes the copy.py

    file in server-A

    .
  • copy.py

    copies the file source.txt

    to a file server-A

    in server-B

    via SFTP.

When I run copy.py

from server-A

, it works correctly. But when I run paramiko_test.py

from localServer

(which indirectly executes copy.py

in server-A

) it doesn't work!

From the logs I found out that there is a successful connection from server-A

to server-B

, but after that the SFTP part doesn't work!

Question: Can we call the SFTP client in the SFTP client? Is there a better way to copy files between the two servers?

Please help me where I am going wrong.

server-A, file: copy.py:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<server-B-IP>', username='serverB', password='passwd')

print "connected successfully!"

sftp = ssh.open_sftp()
print sftp
sftp.put('source.txt','/home/serverB/destination.txt' )
sftp.close()
print "copied successfully!"

ssh1.close()
exit()

      

localServer, paramiko_test.py:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<server-A-IP>', username='serverA', password='passwd')

print "connected successfully!"

stdin, stdout, stderr = ssh.exec_command("python /home/username/copy.py")

print stdout.readlines()

print "copied successfully!"

ssh.close()
exit()

      

Conclusion stderr.readlines()

:

Traceback (most recent call last):
 File "/home/sitaram/sumanth/test_progs/copy.py", line 12, in <module>
 sftp1.put('./sumanth_temp.txt','/home/ncloudadmin/sumanth.txt' ) 
 File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 558, in put
 file_size = os.stat(localpath).st_size
OSError: [Errno 2] No such file or directory: './sumanth_temp.txt'

      

+3


source to share


1 answer


The question is a year, so it's probably not relevant anymore, but maybe it will be useful for others. The problem is in your copy.py.

sftp.put('source.txt','/home/serverB/destination.txt' )

      



where is source.txt located? Either give the full path or if the file is always in the same directory as copy.py you can change your paramiko_test.py

ssh.exec_command("cd /home/username/; python /home/username/copy.py")

      

+5


source







All Articles