#!/usr/bin/env python # -*- coding: utf-8 -*- import pexpect import time import traceback def doRsync(user,passwd,ip,srcDir,dstDir,timeout=3600): cmd = "rsync -azPq --delete {srcDir} {rUser}@{rHost}:{dstDir}".format(rUser = user,rHost=ip,srcDir=srcDir,dstDir=dstDir) try: ssh = pexpect.spawn(cmd,timeout=timeout) i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5) if i == 0 : ssh.sendline(passwd) elif i == 1: ssh.sendline('yes') ssh.expect('password: ') ssh.sendline(passwd) ssh.expect(['#',pexpect.TIMEOUT,pexpect.EOF]) ssh.close() except : print traceback.format_exc() pass if __name__ == '__main__': doRsync("tpuser","12345","10.1.1.230","/data/local/backupdir/","/data/local/backupdir/")
參考連接:html
https://www.cnblogs.com/MikeZhang/p/rsyncExample_20160818.html
http://blog.csdn.net/csapr1987/article/details/7692455
python
疑問1:ssh
pexpect.spawn的對象child調用read()時觸發TIMEOUT異常 ssh遠程操做ide
read() 須要程序回話結束而且返回EOF才能讀取剩下的全部內容spa
詳細可見 pexpect模塊參數解釋.net
參考連接:https://blog.51cto.com/749807/1566258 code
pexpect模塊參數解釋orm