【问题标题】:How to include regular expression in tn.read_until()?如何在 tn.read_until() 中包含正则表达式?
【发布时间】:2015-03-05 14:46:53
【问题描述】:
import telnetlib
tn = telnetlib.Telnet(IP)
tn.read_until("abcd login: ") --> This is only to match a particular pattern

tn.read_until() 中可以包含通用模式吗? 例如:提示可以是"xyz login: " , "abcd login: "

在正则表达式中,我们使用 re.match('(.*)login: ',prompt) 。但我不这么认为,这适用于tn.read_until(),因为它期望的参数本身就是一种模式。有什么办法处理吗?

【问题讨论】:

  • 你试过expect而不是read_until吗?

标签: python regex telnet telnetlib


【解决方案1】:

Telnet.expect 接受正则表达式列表:

tn.expect([r"\w+ login: "])

【讨论】:

  • 可以在一个脚本中同时使用 tn.expect() 和 tn.read_until() 吗? tn.expect() 不适用于 "Password:" 和 "user@abcd:>" 、 "user@xyz:>" 提示。表示超时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多