tcsh] jthimmai@musxoile24:~/jthimmai> cat monitor.py
import pexpect, sys
child = pexpect.spawn ('ssh -l jthimmai musxoile24')
i = child.expect (['Terminal type', '[#\:] '])
if i==0:
print'Login OK... need to send terminal type.'
child.sendline('vt100')
child.expect('[#\:] ')
elif i==1:
print'Login OK.'
print'Shell command prompt', child.after,child.before
child.sendline('uname -a')
#child.sendline('ls -lrt ')
child.expect('[#\:]')
print child.before
[Linux] [tcsh] jthimmai@musxoile24:~/jthimmai>
tcsh] jthimmai@musxoile24:~/jthimmai> /opt/python_2.7.2/bin/python monitor.py
Login OK.
Shell command prompt : Last login
Fri Mar 13 07
[Linux] [tcsh] jthimmai@musxoile24:~/jthimmai>
答案 0 :(得分:0)
如果您的“欢迎信息”与我的相同,则其中包含冒号 - 您正在寻找符合冒号的模式的提示。
这就是child.before
打印Fri Mar 13 07
的原因 - 因为欢迎信息是这样的:
Last login: Fri Mar 13 02:44:19 2015 from somehost.somedomain
^
您可以在if语句中更改模式以包含空格(与之前一样)
prompt = '[#\:] ' # At the beginning, so you don't have to type it every time
#...
child.expect('[#\:]') # This is inside your elif i==1 block
# to
child.expect(prompt)
哪个应该有帮助。