Python - 在两个远程服务器之间传输文件,执行 python 脚本 [英] Python - Transfer a file between two remote servers, excecuting a python script

查看:424
本文介绍了Python - 在两个远程服务器之间传输文件,执行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在两个服务器之间从 localServer 复制文件,比如从 server-Aserver-B.我在 python 中使用 paramiko 包.

I am trying to copy a file between two servers from a localServer, say from server-A to server-B. I am using paramiko package in python.

所以有三个服务器,分别是localServerserver-Aserver-B.请看下面的代码,这是不言自明的,请让我知道我哪里出错了.

So there are three servers namely, localServer, server-A and server-B. Please see the below code, this is self explanatory and please let me know where I am going wrong.

我使用的算法:

  1. 我正在尝试从 localServer 运行 paramiko_test.py 文件.
  2. paramiko_test.py 执行 server-A 中的 copy.py 文件.
  3. copy.py 使用 SFTP 将 server-A 中的 source.txt 文件复制到 server-B.
  1. I am trying to run paramiko_test.py file from localServer.
  2. paramiko_test.py executes copy.py file in server-A.
  3. copy.py copies file source.txt file in server-A to server-B using SFTP.

当我从 server-A 运行 copy.py 时,它工作正常.但是当我从 localServer 运行 paramiko_test.py(它在 server-A 中间接执行 copy.py)时,它不起作用!

When I run copy.py from server-A, it is working correct. But when I run paramiko_test.py from localServer (which indirectly executes copy.py in server-A), it is not working!

从日志中,我知道从 server-Aserver-B 的连接成功,但之后 SFTP 部分不起作用!

From logs, I got to know that there is a successful connection from server-A to server-B, but after that the SFTP part is not working!

问题:我们可以在 SFTP 客户端内调用 SFTP 客户端吗?有没有更好的方法在两台服务器之间复制文件?

Question: Can we invoke a SFTP client within a SFTP client? Is there any better way to copy files between two servers?

请帮助我哪里出错了.

server-A,file:copy.py:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<server-B-IP>', username='serverB', password='passwd')

print "connected successfully!"

sftp = ssh.open_sftp()
print sftp
sftp.put('source.txt','/home/serverB/destination.txt' )
sftp.close()
print "copied successfully!"

ssh1.close()
exit()

localServer,paramiko_test.py:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<server-A-IP>', username='serverA', password='passwd')

print "connected successfully!"

stdin, stdout, stderr = ssh.exec_command("python /home/username/copy.py")

print stdout.readlines()

print "copied successfully!"

ssh.close()
exit()

stderr.readlines() 的输出是:

Traceback (most recent call last):
 File "/home/sitaram/sumanth/test_progs/copy.py", line 12, in <module>
 sftp1.put('./sumanth_temp.txt','/home/ncloudadmin/sumanth.txt' ) 
 File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 558, in put
 file_size = os.stat(localpath).st_size
OSError: [Errno 2] No such file or directory: './sumanth_temp.txt'

推荐答案

这个问题已经有一年了,所以可能不再相关,但也许对其他人有用.问题出在您的 copy.py 中.

Question is year old, so probably not relevant anymore, but maybe it will be useful to others. The problem is in your copy.py.

sftp.put('source.txt','/home/serverB/destination.txt' )

source.txt 位于何处?要么提供完整路径,要么如果文件总是位于与 copy.py 相同的目录中,你可以修改你的 paramiko_test.py

where is source.txt located? Either provide full path, or if the file is always located in in the same dir as copy.py you could modify your paramiko_test.py

ssh.exec_command("cd /home/username/; python /home/username/copy.py")

这篇关于Python - 在两个远程服务器之间传输文件,执行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆