[root@node110 pexpect]# cat simple3.py
import pexpect
import sys
ip="192.168.198.121"
user="root"
passwd="123456"
target_file="/etc/passwd"
child = pexpect.spawn('/usr/bin/ssh', [user+'@'+ip])
fout = file('mylog.txt','w')
child.logfile = fout
try:
child.expect('(?i)password')
child.sendline(passwd)
child.expect('#')
child.sendline('tar -czf /root/passwd.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',[user+'@'+ip+':/root/passwd.tar.gz','/root'])
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"
2017-04-10
远程打包文件并备份到本地
评论
发表评论
姓 名: