Alperen Üretmen
Alperen Üretmen

Reputation: 317

scp between two remote servers in Python

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:

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

Answers (1)

Martin Prikryl
Martin Prikryl

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:

Upvotes: 1

Related Questions