2017-07-30 509 views
0

我想远程登录到远程服务器并尝试获取响应。在python中使用telnet.expect的问题2.7

我之前使用telnet.read_until来匹配是否出现prompt/pattern,但是即使没有匹配,read_until也会返回所有内容。我想用telnet.expect,但我得到的错误

下面是代码

com = re.compile("\#") # is the prompt 
tn.write("somecommand" + "\n") 
res = tn.expect(com, 10) 

错误,我得到的是

File "reg.txt", line 23, in login 
    res = tn.expect(com, 10) 
    File "C:\Python27\lib\telnetlib.py", line 593, in expect 
    list = list[:] 
TypeError: '_sre.SRE_Pattern' object is not subscriptable 

回答

1

telnetlib.expect()需要list作为第一个参数,你给一个SRE_pattern

telnetlib documentation

Telnet.expect从正则表达式的列表(列表,超时=无)

读,直到一个匹配。

第一个参数是正则表达式列表,可以是编译的(正则表达式对象)也可以是未编译的(字节字符串)。可选的第二个参数是以秒为单位的超时值;默认是无限期阻止。

[...]


com = re.compile("\#") # is the prompt 
tn.write("somecommand" + "\n") 
res = tn.expect([com], 10) 

如若作品(的差异是expect([com]),而不是expect(com))。

+0

谢谢。它的工作 – Nitesh