在 python pexpect 中,我想过滤输出。例如,在下面的代码中,我只想打印日期。
#!/usr/bin/env python
import pexpect,time
p=pexpect.spawn('ssh myusername@192.168.151.80')
p.expect('Password:')
p.sendline('mypassword')
time.sleep(2)
p.sendline('date')
p.expect('IST')
current_date = p.before
print 'the current date in remote server is: %s' % current_date
实际输出:
the current date in remote server is:
Last login: Thu Aug 23 22:58:02 2012 from solaris3
Sun Microsystems Inc. SunOS 5.10 Generic January 2005
You have new mail.
welcome
-bash-3.00$ date
Thu Aug 23 23:03:10
预期输出:
the current date in remote server is: Thu Aug 23 23:03:10