#!/usr/bin/env puthonimport pexpectimport sys# Define target host r_ip= "Ip_addrss" r_user= "USERNAME" r_passwd= " PASSWORD "#定义目标主机nginx日志文件target_file ="/path/to/nginx_access.log "#运行ssh命令child = pexpect.spawn ('/usr/ Bin/ssh ', [r_user+ ' @ ' +r_ip]) #输入输出写入日志文件fout = file (' Mylog.txt ', ' W ') child.logfile = Fouttry: child.expect (' (? i) password ') #匹配password字符串, (? i) case insensitive Child.sendline (R_PASSWD) child.expect (' # ') child.sendline (' Tar -czf /path/to/nginx_access.tar.gz ' +target_file) #打包日志文件 child.expect (' # ') print child.before child.sendline (' exit ') fout.close () except eof: #定义异常处理 print "expect eof "except timeout: #定义超时处理 print " Expect timeout "child = Pexpect.spawn ('/usr/BIN/SCP ', [r_user+ ' @ ' +r_ip+ ':/data/nginx_access.tar.gz ', ' Home ']) #scp命令拷贝文件fout = file (' Mylog.txt ', ' a ') child.logfile = fouttry: child.expect (' (? i) password ') child.sendline (passwd) child.expect (pexpect. EOF) except eof: print "expect eof" except timeout: print "Expect timeout"
Automatic package download remote files