python实现scp文件或文件夹传输功能
python实现scp文件或文件夹传输功能。
·
代码
#!/usr/bin/python
#coding:utf-8
import sys,re
import os
import subprocess
#scp file to remote node.
def scpFileToRemoteNode(user, ip, password, localsource, remotedest, port=22):
SCP_CMD_BASE = r"""
expect -c "
set timeout 10 ;
spawn scp -P {port} -r {localsource} {username}@{host}:{remotedest} ;
expect *assword* {{{{ send {password}\r }}}} ;
expect *\r ;
expect \r ;
expect eof
"
""".format(username=user, password=password, host=ip, localsource=localsource, remotedest=remotedest, port=port)
SCP_CMD = SCP_CMD_BASE.format(localsource=localsource)
# print("execute SCP_CMD: ", SCP_CMD)
p = subprocess.Popen(SCP_CMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
p.communicate()
os.system(SCP_CMD)
#scpFileToRemoteNode("root", "192.168.156.72", "密码", "/tmp/var/log", "/etc/", 22)
scpFileToRemoteNode("test", "172.28.9.50", "test", "/home/test/aitest", "/home/test", 22)
scpFileToRemoteNode("test", "172.28.9.50", "test", "/home/test/aitest/a.txt", "/home/test", 22)
更多推荐
已为社区贡献1条内容
所有评论(0)