什么是忽略pexpect中SSH会话的垃圾输出的好方法?
我想自动登录到在会话开始时输出垃圾的主机(由于配置文件脚本中的错误我无法更正)。不幸的是,垃圾邮件包含我多次寻找的提示字符串。有没有一种优雅的方法来等待输出稳定?
换句话说,最多等待 XXX 毫秒最后出现提示字符串。
例如:
prompt = "$ "
child = pexpect.spawn("ssh [email protected]")
# clever stuff here to skip zero or more prompt strings
child.expect_exact(prompt)
I want to automate a login to a host which outputs junk at the start of the session (due to errors in the profile script which I am unable to correct). Unfortunately the junk contains the prompt string I'm looking for, several times over. Is there an elegant way to wait for the output to settle?
In other words, wait at most XXX mS for the last occurrence of the prompt string.
For example:
prompt = "$ "
child = pexpect.spawn("ssh [email protected]")
# clever stuff here to skip zero or more prompt strings
child.expect_exact(prompt)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有发现我问的有关等待输出结算的特定问题的答案,但是对于获得真正提示的问题有一个相对简单的解决方案,该问题不涉及任何等待,或者通过PEXPEPT扫描读取缓冲器寻找图案。
只需回应登录会话连接后已知的内容,然后等待将其发送回去即可。
我在Echo命令中放置了双空间(ECHO在输出中删除),因此我不必匹配键盘Echo Echo Text。这似乎在.cshrc或配置文件脚本产生的任何垃圾之后输出我的唯一文本,因此我只需等待这个唯一的字符串即可跳过额外的提示字符串。我不能确定这在所有情况下都会起作用,但似乎适用于我的。
I found no answer to the specific question I was asking about waiting for the output to settle, however there is a relatively simple solution to the problem of obtaining the real prompt, which doesn't involve any waiting, or scanning through the pexpect read buffer looking for patterns.
Just echo something known into the login session once it's connected, and wait for it to be sent back.
I put double-spaces in the echo command (which echo removes in the output) so I don't have to match the keyboard echo text. This seems to output my unique text after any junk produced by .cshrc or profile scripts, so I skip over the extra prompt strings just by waiting for this unique string. I can't be certain this will work in all cases, but seems to work for mine.