【问题标题】:Cannot Send Long Command in bash Script Expect无法在 bash 脚本期望中发送长命令
【发布时间】:2016-09-06 06:10:29
【问题描述】:

我在使用我的期望脚本发送长命令时遇到问题。我正在使用 telnet 访问路由器:

#! /usr/bin/expect -f
#
#

spawn telnet 192.168.0.5

expect {
  "login:"
    {
        send "admin\r"
        expect "Password:"
        send "admin\r"

        expect ">"
        send "enable\r"
        expect "Password:"
        send "\r"

        # My Command
        expect "#"
        send "terminal length 0\r"
        expect "#"
        send "show running-config | include ip nat pool-group\r"
    }
}

expect "#"
send "exit\r"

我在php中使用exec来运行上面的脚本

<?php
exec(mybinfile , $returnvalue);
print_r($returnvalue);

当我检查输出时,似乎发送了不正确的命令。但是,它仍然有效。为什么会这样输出?

结果如下:

[14] => R1#terminal length 0
[15] => R1#show running-config | include ip nat pool-gro$ng-config | include ip nat pool-grou          $ng-config | include ip nat pool-group

我尝试减少命令中的字符数,如下例所示,这很好:

show running-config | include ip nat pool-gro

如果这是我在print_r 中看到的输出,我会遇到问题吗?

【问题讨论】:

  • Will I encounter problems if that is the output I can see in print_r? 不会。不会有问题,这就是 expect 的工作方式。字符串越长,越乱。
  • 它是删除设备、telnet 本身以及 expect 如何从 telnet 捕获输出的组合。这与“期望如何工作”和“胡言乱语”无关。当您“手动”远程登录到该设备时,某些文本是否显示为粗体或突出显示?

标签: php exec expect telnet


【解决方案1】:

除了发出terminal length 0 命令之外,您还可以发出terminal width 511 命令来防止换行。这为我们解决了类似的问题。

来自the documentation

要设置控制台会话期间显示信息的宽度,请在全局配置模式下使用 终端宽度 命令。要禁用,请使用此命令的 no 形式。

终端宽度

无终端宽度

另请参阅 ServerFault 上的 this question

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2016-10-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多