您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下python如何实现执行scp命令拷贝文件及文件夹到远程主机的目录,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
系统环境centos7
python2.7
先在操作系统安装expect
[root@V71 python]# vi 3s.py #!/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 300 ; 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) 执行脚本: [root@V71 python]# python 3s.py execute SCP_CMD: expect -c " set timeout 300 ; spawn scp -P 22 -r /tmp/var/log root@192.168.156.72:/etc/ ; expect *assword* { send 密码\r } ; expect *\r ; expect \r ; expect eof " spawn scp -P 22 -r /tmp/var/log root@192.168.156.72:/etc/ root@192.168.156.72's password: messages 100% 802KB 802.1KB/s 00:00
看完了这篇文章,相信你对“python如何实现执行scp命令拷贝文件及文件夹到远程主机的目录”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。