什么是忽略pexpect中SSH会话的垃圾输出的好方法?

发布于 2025-01-17 23:10:29 字数 432 浏览 3 评论 0原文

我想自动登录到在会话开始时输出垃圾的主机(由于配置文件脚本中的错误我无法更正)。不幸的是,垃圾邮件包含我多次寻找的提示字符串。有没有一种优雅的方法来等待输出稳定?

换句话说,最多等待 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2025-01-24 23:10:29

我没有发现我问的有关等待输出结算的特定问题的答案,但是对于获得真正提示的问题有一个相对简单的解决方案,该问题不涉及任何等待,或者通过PEXPEPT扫描读取缓冲器寻找图案。

只需回应登录会话连接后已知的内容,然后等待将其发送回去即可。

child.sendline("echo my  unique  string")
child.expect_exact("my unique string")
child.expect_exact("$ ")

我在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.

child.sendline("echo my  unique  string")
child.expect_exact("my unique string")
child.expect_exact("$ ")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文