2

I am trying to connect a remote server using Paramiko and send some files to other remote server. I tried the code below, but it didn't work. I checked all connections, username and password parameters, they don't have any problem. Also the file which I want to transfer exist in first remote server in proper path.

The reason why I don't download files to my local computer and upload to second server is, connection speed between two remote servers is a lot faster.

Things that I tried:

  • I set paramiko log level to debug, but couldn't find any useful information.

  • I tried same scp command from first server to second server from command line, worked fine.

  • I tried to log by data = stdout.readlines() after stdin.flush() line but that didn't log anything.

import paramiko
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("10.10.10.10", 22, username='oracle', password='oracle', timeout=4)

stdin, stdout, stderr = s.exec_command(
        "scp /home/oracle/myFile.txt oracle@10.10.10.20:/home/oracle/myFile.txt")

stdin.write('password\n')
stdin.flush()
s.close()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alperen Üretmen
  • 307
  • 1
  • 13

1 Answers1

1

You cannot write a password to the standard input of OpenSSH scp.

Try it in a shell, it won't work either:

echo password | scp /home/oracle/myFile.txt oracle@10.10.10.20:/home/oracle/myFile.txt

OpenSSH tools (including scp) read the password from a terminal only.

You can emulate the terminal by setting get_pty parameter of SSHClient.exec_command:

stdin, stdout, stderr = s.exec_command("scp ...", get_pty=True)
stdin.write('password\n')
stdin.flush()

Though enabling terminal emulation can bring you unwanted side effects.

A way better solution is to use a public key authentication. There also other workarounds. See How to pass password to scp? (though they internally have to do something similar to get_pty=True anyway).


Other issues:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992