无法从远程服务器获取输出

2024-05-12 11:19:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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>

Tags: pychildtypeloginokshellcommandmonitor
1条回答
网友
1楼 · 发布于 2024-05-12 11:19:38

如果您的“欢迎消息”与我的相同,那么它有冒号,并且您正在使用与冒号匹配的模式查找提示。你知道吗

这就是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)

这应该会有帮助。你知道吗

相关问题 更多 >