title: Python - Transfer a file between two remote servers, excecuting a python script
body: <p>I am trying to copy a file between two servers from a <code>localServer</code>, say from <code>server-A</code> to <code>server-B</code>. I am using paramiko package in python.</p>
  
  <p>So there are three servers namely, <code>localServer</code>, <code>server-A</code> and <code>server-B</code>. Please see the below code, this is self explanatory and please let me know where I am going wrong.</p>
  
  <p>Algorithm I am using:</p>
  
  <ol>
  <li>I am trying to run <code>paramiko_test.py</code> file from <code>localServer</code>.</li>
  <li><code>paramiko_test.py</code> executes <code>copy.py</code> file in <code>server-A</code>.</li>
  <li><code>copy.py</code> copies file <code>source.txt</code> file in <code>server-A</code> to <code>server-B</code> using SFTP.</li>
  </ol>
  
  <p>When I run <code>copy.py</code> from <code>server-A</code>, it is working correct. But when I run <code>paramiko_test.py</code> from <code>localServer</code> (which indirectly executes <code>copy.py</code> in <code>server-A</code>), it is not working!</p>
  
  <p>From logs, I got to know that there is a successful connection from <code>server-A</code> to <code>server-B</code>, but after that the SFTP part is not working!</p>
  
  <p><strong>Question:</strong> Can we invoke a SFTP client within a SFTP client? Is there any better way to copy files between two servers?</p>
  
  <p>Please help me where I am going wrong.</p>
  
  <p><strong>server-A, file:copy.py:</strong></p>
  
  <pre><code>import paramiko
  
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect('&lt;server-B-IP&gt;', 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()
  </code></pre>
  
  <p><strong>localServer, paramiko_test.py:</strong></p>
  
  <pre><code>import paramiko
  
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect('&lt;server-A-IP&gt;', 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()
  </code></pre>
  
  <p>The output of <code>stderr.readlines()</code> is:</p>
  
  <pre><code>Traceback (most recent call last):
   File "/home/sitaram/sumanth/test_progs/copy.py", line 12, in &lt;module&gt;
   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'
  </code></pre>