本文實(shí)例講述了Python實(shí)現(xiàn)的遠(yuǎn)程文件自動(dòng)打包并下載功能。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
在Linux系統(tǒng)集群運(yùn)營(yíng)當(dāng)中,時(shí)常需要批量遠(yuǎn)程執(zhí)行Linux命令,并且雙向同步文件的操作。
本示例通過(guò)使用spawn()方法執(zhí)行ssh、scp 命令的思路來(lái)實(shí)現(xiàn)。
二 代碼
import pexpect import sys ip="192.168.0.104" user="root" passwd="123456" target_file="/data/test.txt" child = pexpect.spawn('/usr/bin/ssh', [user+'@'+ip]) fout = file('mylog.txt','w') child.logfile = fout try: child.expect('password: ') child.sendline(passwd) child.expect('#') child.sendline('tar -czf /data/test.tar.gz '+target_file) child.expect('#') print child.before child.sendline('exit') fout.close() except pexpect.EOF: print "expect EOF1" except pexpect.TIMEOUT: print "expect TIMEOUT1" child = pexpect.spawn('/usr/bin/scp', [user+'@'+ip+':/data/test.tar.gz','/home']) fout = file('mylog.txt','a') child.logfile = fout try: child.expect('(?i)password') child.sendline(passwd) child.expect(pexpect.EOF) except pexpect.EOF: print "expect EOF2" except pexpect.TIMEOUT: print "expect TIMEOUT2"
分享標(biāo)題:Python實(shí)現(xiàn)的遠(yuǎn)程文件自動(dòng)打包并下載功能示例-創(chuàng)新互聯(lián)
URL鏈接:http://m.kartarina.com/article22/ejsjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、微信小程序、網(wǎng)站內(nèi)鏈、移動(dòng)網(wǎng)站建設(shè)、App設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容