远程文件自动打包并下载

远程文件自动打包并下载

[root@server0 318]# cat ftp.py 
#!/usr/bin/python
import pexpect
import sys
ip="172.25.254.2"
user="root"
password="redhat"
try:
	child=pexpect.spawn('/usr/bin/ssh',[user+'@'+ip])
	target_file="/root/Django-1.7.11"
	four=file('mylog.txt','w')
	child.logfile=four		输入输出写道文件里面
	child.expect('(?i)password')		(?i)表示不区别大小写
	child.sendline(password)
	child.expect('#')
	child.sendline('tar -czf /mnt/mimi.tar.gz '+target_file)
	child.expect('#')
	print child.before
	child.sendline('exit')
	four.close()
except:
	print "failed"
child1=pexpect.spawn('/usr/bin/scp',[user+'@'+ip+':/mnt/mimi.tar.gz','/mnt/'])
four1=file('loffile.txt','w')
child1.logfile=four1
child1.expect('(?i)password')
child1.sendline(password)
child1.expect(pexpect.EOF)	匹配缓冲区EOF(结尾),保证文件复制正常完成

原文地址:https://www.cnblogs.com/hanfei-1005/p/5707816.html