Sumit
Sumit

Reputation: 2023

problems in using telnet.expect in python 2.7

I am trying to telnet to remote server and trying to get the response back.

I was using telnet.read_until earlier to match whether prompt/pattern appeared or not but read_until returns everything even if there is no match. I thought of using telnet.expect but i am getting error

Below is the code

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

Error I am getting is

  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

Upvotes: 0

Views: 1200

Answers (1)

Arount
Arount

Reputation: 10403

telnetlib.expect() requires list as first argument, you are giving a SRE_pattern.

From telnetlib documentation:

Telnet.expect(list, timeout=None)

Read until one from a list of a regular expressions matches.

The first argument is a list of regular expressions, either compiled (regex objects) or uncompiled (byte strings). The optional second argument is a timeout, in seconds; the default is to block indefinitely.

[...]


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

Should works (the diff is expect([com]) instead of expect(com)).

Upvotes: 1

Related Questions