有人对 telnetlib.expect() 感到幸运吗?
我正在编写一个库来支持远程登录到远程服务器并运行应用程序。
建立连接、取回数据、解析等一切都进展顺利(至少与通过文本界面与程序通信一样顺利)。
如果正确输入,一个应用程序将更改光标,或者如果失败则保留原始光标(我不编写应用程序,我只需要使用它们。)
当所述应用程序正确启动时,这没有问题:
promptB = "hello(x)# " # Yes, the space at the end is intentional
response = tn_conn.cmd("app_name\n", prompt=promptB)
我想利用提示变化(或缺少提示变化)来检测程序是否启动失败。我认为这将是尝试 telnetlib 的expect() 的绝佳机会,因为expect() 允许传递一个字符串列表以在响应中进行匹配。
但是,我无法让它工作:
promptA = "hello(x)# " # Yes, the space at the end is intentional
promptB = "hello> " # Yes, the space at the end is intentional
tn_conn.write("app_name\n")
which_prompt, mo, response = self.tn_conn.expect([promptA, promptB], timeout=3)
无论应用程序是否成功启动,expect 命令总是超时。
which = "-1"
mo = None
response = "mumble mumble\r\r\n other stuff\r\n\r\nhello# "
文档说可以将字符串或正则表达式对象传递给 Expect (I正在传递一个字符串),所以我错过了一些东西吗?查看 telnetlib 代码表明它调用 re.search(),而不是 re.match(),因此这似乎不是问题。
谁能就我做错的事情提出建议?
编辑 在提示示例中添加了括号,以更好地说明 Expect() 未按预期工作的原因。
I'm writing a library to support telnet'ing to a remote server and running apps.
Things are going swimmingly in establishing a connection, getting data back, parsing, etc. (at least as swimmingly as it can be for communicating with programs via a text interface).
One app will change the cursor if it enters properly, or leave the original cursor if it fails (I don't write the apps, I just have to use them.)
When said app starts up correctly, this works with no problem:
promptB = "hello(x)# " # Yes, the space at the end is intentional
response = tn_conn.cmd("app_name\n", prompt=promptB)
I would like to use the prompt change (or lack of prompt change) to detect whether the program failed to start. I figured this would be a golden opportunity to try telnetlib's expect(), since expect() allows one to pass a list of strings to match in the response.
I cannot, however, get this to work:
promptA = "hello(x)# " # Yes, the space at the end is intentional
promptB = "hello> " # Yes, the space at the end is intentional
tn_conn.write("app_name\n")
which_prompt, mo, response = self.tn_conn.expect([promptA, promptB], timeout=3)
The expect command always times out, whether to apps starts sucessfully or not.
which = "-1"
mo = None
response = "mumble mumble\r\r\n other stuff\r\n\r\nhello# "
The docs say that either a string or a regex object can be passed to expect (I'm passing a string), so am I missing something? A look at the telnetlib code shows that its calling re.search(), not re.match(), so that wouldn't seem to be the issue.
Can anyone please offer suggestions on what I'm doing wrong?
Edit
Added parens to the prompt example to better illustrate why expect() was not working as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要忘记,如果您在 python 中使用正则表达式,您始终可以使用原始 (
r'my string'
) 方法,而不是添加所有转义符;使其更具可读性。Don't forget if you are using the regex in python you can always use the raw (
r'my string'
) method rather than adding in all the escapes; makes it more readable.我还有事要忙。查找 # 或 % 或 $ 提示。至于您的提示,请确保转义特殊字符。 ( )。也许只是为了确定而逃避一切。
I got something to work. Lookig for # or % or $ prompts. As for your prompts, make sure that special characters are escaped. ( ). Maybe escape everthing just to be sure.
在之前的尝试中,我通过在搜索字符串的两端放置
.*
以及对搜索字符串执行re.compile()
来追求正则表达式选项在将其/它们传递给.expect()
之前;一切都没有运气。感谢 jathanism 的建议,我重新检查了正则表达式的使用,这次我认为
expect()
是,呃...在我认为是“字符串”的地方期待“正则表达式”。果然,我的提示字符串中有一些字符期望被视为正则表达式符号 - 准确地说是
()
。转义括号让expect()
完成它的工作。In previous attempts, I had pursued the regex option by placing
.*
at both ends of my search string, as well as doing are.compile()
to the search string before passing it/them to.expect()
; all with no luck.Thanks to jathanism's suggestion, I re-examined using regex, this time with the thought that
expect()
was, er... expecting 'regex' where I was thinking 'string'.Sure enough, there were characters in my prompt string that expect was treating as regex symbols --
()
's to be exact. Escaping the parens letexpect()
do its job.