[paramiko] copying a file between to remote servers
Milan Andric
mandric at gmail.com
Sun Oct 11 09:55:46 PDT 2009
On Sun, Oct 11, 2009 at 6:03 AM, Eric Noulard <eric.noulard at gmail.com> wrote:
> 2009/10/11 Milan Andric <mandric at gmail.com>:
>> Hello,
>>
>> Is it possible to use paramiko to transfer a file between two remote
>> servers? Scp supports this but I could not get it to work with
>> non-standard ssh ports (not 22). Like:
>>
>> scp unprivuser1 at remote1.com:/over/here/xyz.tgz
>> unprivuser2 at remote2.com:/some/dir/
>>
>> This doesn't respect the -P flag for the port.
>>
>> So I'm trying to figure out if paramiko can help me transfer a file
>> between two remote servers without doing the transfer locally first.
>
> Using paramiko you should be able to do it "in-memory" for example
> using 2 SFTPFiles:
> http://www.lag.net/paramiko/docs/paramiko.SFTPFile-class.html
>
> Something like:
> remote1 = SSHClient()
> remote1.load_system_host_keys()
> remote1.connect('remote1.com')
> sftp1 = remote1.open_sftp()
> remote2 = SSHClient()
> remote2.load_system_host_keys()
> remote2.connect('remote1.com')
> sftp2 = remote2.open_sftp()
>
> file1 = sftp1.open("/over/here/xyz.tgz",'r')
> file2 = sftp2.open("/some/dir/xyz.tgz",'w')
>
> then do appropriate loop for reading from file1
> and writing to file2.
> remote1.connect('remote1.com') may be replaced
> with an extra port=<portval> argument.
>
This sounds awesome! I think this is the way to go.
> Concerning ssh/scp, may be you ssh to one remote host
> and launch scp from there? Something like:
>
> ssh unprivuser2 at remote2.com:/some/dir scp
> unprivuser1 at remote1.com:/over/here/xyz.tgz .
The problem with this is that I need to also copy and remove the
private key around from whatever host I want to ssh from. I would like
to avoid that extra management step if possible since it might involve
leaving a stray private key around. So it would look more like :
scp ../private/id_rsa_remote2 -i ../private/id_rsa_remote1
user at remote1.com:/home/user/tmp-id_rsa_remote2
ssh -i ../private/id_rsa_remote1 user at remote1.com "scp
/my/build/file.tgz -i /home/user/tmp-id_rsa_remote2
user at remote2.com:/my/deploy/dir && rm /home/user/tmp-id_rsa_remote2"
Thanks again,
Milan
More information about the paramiko
mailing list