php - 无法在 bash 脚本 Expect 中发送长命令

标签 php exec expect telnet

我在使用 expect 脚本发送长命令时遇到了问题。我正在使用 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 中看到的输出,我会遇到问题吗?

最佳答案

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

来自 the documentation :

To set the width for displaying information during console sessions, use the terminal width command in global configuration mode. To disable, use the no form of this command.

terminal width columns

no terminal width columns

另见 this question在 ServerFault 上。

关于php - 无法在 bash 脚本 Expect 中发送长命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37152514/

相关文章:

bash: 查找 -exec 和文件名

基于 TCL 的 Expect 的 Ruby 版本?

linux - 我如何通过 ssh 进入一台机器,等待提示,然后向它发送一堆命令?

php - 如何使用 MySQL 和 PHP 打印单个单元格的值?

php - 我如何提交位于 <div style ="display:none"> 内的输入?

python - 如何从另一个 Python 文件运行不在目录中的 Python 文件?

Python 3.3 配置文件 - 字典被随机的东西填充

php - Codeigniter 不显示页面

php - 用户角色(例如管理员、用户)进入表而不是用户名

Python PEXPECT - 超时