Python實現(xiàn)的遠程文件自動打包并下載功能示例-創(chuàng)新互聯(lián)

本文實例講述了Python實現(xiàn)的遠程文件自動打包并下載功能。分享給大家供大家參考,具體如下:

10多年專注成都網(wǎng)站制作,成都定制網(wǎng)站,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)站,高端網(wǎng)頁制作,對邊坡防護網(wǎng)等多個方面,擁有多年的網(wǎng)站維護經(jīng)驗。

一 點睛

在Linux系統(tǒng)集群運營當中,時常需要批量遠程執(zhí)行Linux命令,并且雙向同步文件的操作。

本示例通過使用spawn()方法執(zhí)行ssh、scp 命令的思路來實現(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"

分享標題:Python實現(xiàn)的遠程文件自動打包并下載功能示例-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article22/ejsjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、微信小程序、網(wǎng)站內(nèi)鏈移動網(wǎng)站建設(shè)、App設(shè)計動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)