4
import telnetlib
tn = telnetlib.Telnet(IP)
tn.read_until("abcd login: ") --> This is only to match a particular pattern

Can a generic pattern be included in tn.read_until() ? Ex: prompt can be "xyz login: " , "abcd login: " etc

In regular expression, we use re.match('(.*)login: ',prompt) . But i don't think so, this works in tn.read_until() because the parameter it expects itself is a pattern. Is there any way to handle it ?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Jackie James
  • 785
  • 8
  • 15
  • 28
  • Have you tried [`expect`](https://docs.python.org/2/library/telnetlib.html#telnetlib.Telnet.expect) instead of `read_until`? – Kevin Jan 07 '15 at 16:31

1 Answers1

6

Telnet.expect accepts a list of regular expressions:

tn.expect([r"\w+ login: "])
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Can both tn.expect() & tn.read_until() be used in one script ? tn.expect() not working for "Password: " and "user@abcd:>" , "user@xyz:>" prompts. Says as timeout. – Jackie James Jan 07 '15 at 16:56