2

I have 3 machines:

  • Server A (build machine where code will run)
  • Server B (Linux)
  • Server C (Linux)

My code is running in Server A. I want to ssh to Server B and then copy files to Server C. I want to use password of Server B and Server C and NOT keys.

In my case I am able to write code to copy files from local machine to remote using Paramiko. I looked for many solutions on stackoverflow like below:

import paramiko
from scp import SCPClient
def createSSHClient(server, port, user, password):
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(server, port, user, password)
    return client
ssh = createSSHClient(server, port, user, password)
scp = SCPClient(ssh.get_transport())
cp.get(r'/nfs_home/appers/xxxx/test2.txt', r'C:\Users\xxxx\Desktop\MR_Test')

But again files are getting copying from local (where code is run) to remote and not remote to remote.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Pankaj Kumar
  • 61
  • 2
  • 9
  • I tried to ssh using paramiko and send the scp command and then password using stdin. But problem is scp (and so ssh in back end) accepts password directly from terminal and not from standard input ( unlike 'sudo -S ') – Pankaj Kumar Mar 26 '18 at 09:36

1 Answers1

1

You cannot use SCPClient class for this.

You have to login to Server B and run scp command-line client there to upload a (local - as of server B) file to Server C.

See python paramiko run command.

You will have problems passing password to scp. You better use keys. If you do not want to use keys, you have to use some of the hacks described for example here:

(and in zillions of other similar questions of desperates who insist on not using keys)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • scp did not work for me because of reasons mentioned in my comment on question (scp accepts password directly from terminal and not from standard input ). Also i dont want to use 'sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path' because will need to install sshpass on both client and server machines. Also in zillions of questions i could not get proper answer for this scenario. Dont know if this is possible using paramiko . – Pankaj Kumar Mar 26 '18 at 14:46
  • I'm aware of all that. Use keys! If you still insists on hacking this with a password, use `get_pty=True` when calling `exec_command`. – Martin Prikryl Mar 26 '18 at 14:52