带 Expect 的 Telnet 自动化:身份验证速度慢?

发布于 2024-11-05 10:08:02 字数 790 浏览 5 评论 0原文

我正在使用 Telnet 向 Mikrotik 路由器发送命令。

telnet 192.168.100.100 -l admin
Password: pass1234
[admin@ZYMMA] > /interface pppoe-server remove [find user=aspeed13]
[admin@ZYMMA] > quit

效果很好。

现在我想使用 Expect tcl 脚本自动执行它:

#!/usr/bin/expect --
spawn telnet 192.168.100.100
expect "Login:"
send "admin\r"
expect "Password:"
send "pass1234\r"
expect "\[admin@ZYMMA\] >"
send "/interface pppoe-server remove \[find user=aspeed13\]\r"
expect "\[admin@ZYMMA\] >"
send "quit\r"

它可以工作,但在身份验证(第 6 行:发送“pass1234\r”)之后,当路由器 CLI 加载时,它会冻结约 10 秒,并显示以下字符^[[?6c^[[24;3R 然后脚本运行正常。

我的问题是为什么 Telnet 在手动访问时加载速度很快,而在通过 Expect 脚本访问时则花费太多时间?我在有关 telnet 自动化的论坛上读到,他们说 telnet 很慢,但由于手动它太快了,为什么需要时间来加载期望?

I'm sending a command to a Mikrotik router using Telnet.

telnet 192.168.100.100 -l admin
Password: pass1234
[admin@ZYMMA] > /interface pppoe-server remove [find user=aspeed13]
[admin@ZYMMA] > quit

It works fine.

Now I want to automate it using expect tcl script:

#!/usr/bin/expect --
spawn telnet 192.168.100.100
expect "Login:"
send "admin\r"
expect "Password:"
send "pass1234\r"
expect "\[admin@ZYMMA\] >"
send "/interface pppoe-server remove \[find user=aspeed13\]\r"
expect "\[admin@ZYMMA\] >"
send "quit\r"

It works, but after authentication (line 6: send "pass1234\r") when the Router CLI is loading it freezes for ~10seconds with the following characters ^[[?6c^[[24;3R
Then the scripts runs ok.

My question is why Telnet loads fast when accessed manually and it takes too much time when accessed via expect script? I read in forums about telnet automation they say telnet is slow, but since manually it's too fast why it takes time to load with expect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

沫尐诺 2024-11-12 10:08:02

您所看到的是终端协商的反作用,这是因为您没有在真实终端中运行。 (严格来说,您是 - 这是期望的魔力 - 但它的行为并不像普通终端那样。)

最简单的解决方法是在生成 telnet 会话之前将终端设置为其他内容,例如:

#!/usr/bin/expect --
set env(TERM) dumb
spawn telnet 192.168.100.100
# Rest of your script goes here ...

或者,您可以尝试正确响应进入 VT102 模式的请求和光标位置的报告(感觉工作量很大),或者您可以重写代码,以便它执行 interact 内的所有操作(这将另一端与您在其中运行的真实终端连接)。但如果设置一个环境变量可以解决这个问题,为什么还要这么麻烦呢?

(注意:我建议在这里将终端设置为“哑”,但关键是您需要最愚蠢的终端才能工作。哑终端是理想的选择,因为它们完全 愚蠢,很容易冒充他们……)

What you're seeing is blow-back from terminal negotiation, which is because you're not running in a real terminal. (Strictly, you are – that's expect's magic – but it's not behaving as a normal terminal does.)

The easiest fix is to set the terminal to something else before spawning the telnet session, e.g.:

#!/usr/bin/expect --
set env(TERM) dumb
spawn telnet 192.168.100.100
# Rest of your script goes here ...

Alternatively, you could try to respond correctly to the request to enter VT102 mode and the report of the cursor location (which feels like a lot of work) or you could rewrite your code so that it does everything inside interact (which connects the other end with the real terminal that you're running inside). But if setting an environment variable fixes it, why go to all that extra hassle?

(NB: I suggest setting the terminal to dumb here, but the key is that you want the stupidest terminal that works. Dumb terminals are ideal, because they're just about totally stupid, making it easy to pretend to be them…)

做个少女永远怀春 2024-11-12 10:08:02

我的回答可能已经太晚了。这是“Telnet autoconfig 命令”...我遇到了这个问题,并在 Mikrotik Wiki 上找到了这个解决方案:

在登录名后添加 +t。将此开关自动检测关闭。

例子:
发送“admin+t\r”

效果很好,而不是按预期登录后“等待 cca 10 秒”。

Mikrotik WiKi 帮助中有更多“开关”的链接:
http://wiki.mikrotik.com/wiki/Manual:Console_login_process#FAQ

PS:对不起我的英语。

My answer is possibly too late. This is "Telnet autoconfig command"...I was this problem and found at Mikrotik Wiki this solution:

Add +t after login name. This switch autodetect to off.

Example:
send "admin+t\r"

It is works great and not "wait cca 10 sec" after login by expect.

There is link to Mikrotik WiKi help with more "switches":
http://wiki.mikrotik.com/wiki/Manual:Console_login_process#FAQ

P.S.: Sorry for my English.

站稳脚跟 2024-11-12 10:08:02

您是否尝试使用 netcat 并启用了 telnet 模拟?

Did you try with netcat, with telnet emulation enabled?

雪化雨蝶 2024-11-12 10:08:02

回答有点晚了。
但如果你想用expect来加速你的字符输入。
尝试使用“autoexpect”命令生成脚本,这将保存
在同一目录中名为“script.exp”的文件中进行交互
您运行了命令。

例如:
cd $HOME
自动预期 telnet 192.168.100.100
# 这里还有一些 telnet 命令
exit

以上所有命令都会保存在~/script.exp
关于Tcl,我不知道这个脚本是否可以通过tcl运行。

A little bit late to answer.
But if you want to speed up your character input with expect.
Try to generate the script with "autoexpect" command, which will save the
interaction in a file named "script.exp" in the same directory from where
you ran the command.

For instance:
cd $HOME
autoexpect telnet 192.168.100.100
# some more telnet commands here
exit

All the above commands will be saved in ~/script.exp
About Tcl, I don't know if ths script can be ran via tcl.

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