pexpect实现远程操作

#!/usr/bin/python
import pexpect
import sys
sip="ssh [email protected]"
try:
child=pexpect.spawn(sip)
res = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'password:'])
if res==0:
child.sendline('yes')
child.expect('password:')
child.sendline('123456')
child.expect('#')
child.sendline('ls -l')
child.sendline('df -h')
print child.before
child.sendline('exit')
child.interact()
except pexpect.EOF,pexpect.TIMEOUT:
print 'timeout...'

猜你喜欢

转载自www.cnblogs.com/mmdln/p/9050157.html
今日推荐