【发布时间】:2019-07-03 17:33:15
【问题描述】:
我正在尝试通过期望脚本从交换机获取配置详细信息。
当我通过 shell 手动调用时,下面的部分工作正常。
但是当通过同样的调用时,php(构建一个 Web 界面来调用它)不知何故 expect_out(buffer) 没有填满所有行,我的匹配失败了。
我正在尝试匹配是否设置了配置位。
手动调用时代码再次起作用,只是通过 php 这以某种方式无法填充 expect_out(buffer) :( :(
工作代码。
#!/usr/bin/expect
set ip ROUTER_IP
set uname USER_NAME
set psd PASSWORD
set log_file -a "/tmp/script.log"
spawn /usr/bin/ssh $uname@$ip
expect {
"ord:" {
send "$psd\r"
expect {
"> " {
send "\r"
set cmd "show configuration | display set | match class | match rancid"
set values $expect_out(buffer)
send_log "\nbefore trim n match -$values-\n"
set values [string trim $values]
send_log "\nbefore match -$values-\n"
set found [regexp {match rancid\s+(.*)\s+\r\n\{.*} $values match px]
if { $found == 1 && $px != "" } {
puts "Rancid Exists... !!!"
send "exit\r"
exit 1
} else {
puts "Coundnt find Rancid ... !!!"
send "exit\r"
exit 2
}
}
"denied: " {
puts "ERROR: Access Denied"
exit 2
}
}
}
}
我是这样通过 php 调用的,
$res=shell_exec("/usr/bin/expect $scriptPath ");
我可以看到,设置为 $values 的 expect_out(buffer) 被 log_file 输出的一半命令填充。
成功的 expect_out(buffer),
before trim match -show configuration | display set | match class | match rancid ^M
set system login user rancid class remote-backup^M
^M
{master:1}^M
USER_NAME@SWITCH> -
before match -show configuration | display set | match class | match rancid ^M
set system login user rancid class remote-backup^M
^M
{master:1}^M
USER_NAME@SWITCH>-
当通过 php 调用时,expect_out(buffer) 看起来像这样,
before trim match -show configuration | display set | m^MUSER_NAME@SWITCH> -
before match -show configuration | display set | m^MUSER_NAME@SWITCH>-
不知何故,当期望通过 php 调用脚本时,缓冲区没有所有命令输出并且匹配失败。
谁能指导我可能是什么错误。谢谢。
【问题讨论】: