您好,登錄后才能下訂單哦!
小編給大家分享一下python如何實現(xiàn)執(zhí)行scp命令拷貝文件及文件夾到遠程主機的目錄,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
系統(tǒng)環(huán)境centos7
python2.7
先在操作系統(tǒng)安裝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) 執(zhí)行腳本: [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如何實現(xiàn)執(zhí)行scp命令拷貝文件及文件夾到遠程主機的目錄”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。