代码

#!/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)

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐