自动打包下载远程文件
#!/usr/bin/env puthon import pexpect import sys #定义目标主机 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 = fout try: child.expect('(?i)password')#匹配password字符串,(?i)不区分大小写 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 = fout try: child.expect('(?i)password') child.sendline(passwd) child.expect(pexpect.EOF) except EOF: print "expect EOF" except TIMEOUT: print "expect TIMEOUT"
当前名称:自动打包下载远程文件
路径分享:http://ybzwz.com/article/psdcdp.html