Reputation: 317
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 [email protected]:/home/oracle/myFile.txt")
stdin.write('password\n')
stdin.flush()
s.close()
Upvotes: 2
Views: 1203
Reputation: 202232
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 [email protected]:/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:
s.close()
will likely terminate the transfer. Using stdout.readlines()
will do in most cases. But it may hang, see Paramiko ssh die/hang with big output.AutoAddPolicy
– You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".Upvotes: 1