我正在尝试运行一个调用expect的bash脚本作为守护进程,只要脚本仍然附加到终端,一切都很好。但是当我将脚本移到后台或从crontab执行它时,生成的telnet进程不再与expect脚本通信,调试时出现超时错误,脚本失败。
另一件值得一提的事实是,我遇到这个问题只有一些具有较旧固件版本的设备(升级成本高而且不是一个选项),新设备上的相同脚本运行完美。
非常感谢有关如何克服这个问题的任何反馈。
调试输出如下:
expect version 5.45
argv[0] = /usr/bin/expect argv[1] = -d
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
executing commands from command file
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {9602}
expect: does "" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\n" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to KASH.\r\r\nEscape character is '^]'.\r\r\n" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to KASH.\r\r\nEscape character is '^]'.\r\r\n ****************************************************************\r\n * _______ _________ _______ __ __ __ __ *" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to KASH.\r\r\nEscape character is '^]'.\r\r\n ****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n * / /____ | | | |_____ | | / / \ \ *" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to KASH.\r\r\nEscape character is '^]'.\r\r\n ****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n * / /____ | | | |_____ | | / / \ \ *\r\n * /_______| |_| |_______| |__| /__/ \__\ *\r\n * *\r\n * Welcome to you !!! *\r\n * *\r\n * ( MSG5200 V2.0.3 ) *\r\n ****************************************************************\r\nLogin:" (spawn_id exp6) match glob pattern "Login:"? yes
expect: set expect_out(0,string) "Login:"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Trying 10.102.0.37...\r\r\nConnected to KASH.\r\r\nEscape character is '^]'.\r\r\n ****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n * / /____ | | | |_____ | | / / \ \ *\r\n * /_______| |_| |_______| |__| /__/ \__\ *\r\n * *\r\n * Welcome to you !!! *\r\n * *\r\n * ( MSG5200 V2.0.3 ) *\r\n ****************************************************************\r\nLogin:"
send: sending "root\r" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "Password:"? no
expect: does "rPress any key to continue (Q to quit)" (spawn_id exp6) match glob pattern "Password:"? no
expect: timed out
send: sending "root\r" to { exp6 }
Gate keeper glob pattern for '.*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# ' is '*# '. Activating booster.
expect: does "rPress any key to continue (Q to quit)" (spawn_id exp6) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "rPress any key to continue (Q to quit)\r" (spawn_id exp6) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "rPress any key to continue (Q to quit)\r \roPress any key to continue (Q to quit)" (spawn_id exp6) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: timed out
send: sending "show vdsl2 port 2/4/13 line-config\r" to { exp6 }
Gate keeper glob pattern for '.*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# ' is '*# '. Activating booster.
expect: does "rPress any key to continue (Q to quit)\r \roPress any key to continue (Q to quit)" (spawn_id exp6) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "rPress any key to continue (Q to quit)\r \roPress any key to continue (Q to quit)\r" (spawn_id exp6) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "rPress any key to continue (Q to quit)\r \roPress any key to continue (Q to quit)\r \rhPress any key to continue (Q to quit)" (spawn_id exp6) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: timed out
send: sending "logout\r" to { exp6 }
expect: timed out
脚本从终端运行时调试输出:
expect version 5.45 argv[0] = /usr/bin/expect argv[1] = -d set argc 0 set argv0 "/usr/bin/expect" set argv "" executing commands from command file parent: waiting for sync byte parent: telling child to go ahead parent: now unsynchronized from child spawn: returns {14876}
expect: does "" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\n" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to TI-ATEM-KASH.\r\r\nEscape character is '^]'.\r\r\n" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to TI-ATEM-KASH.\r\r\nEscape character is '^]'.\r\r\n
****************************************************************\r\n * _______ _________ _______ __ __ __ __ *" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to TI-ATEM-KASH.\r\r\nEscape character is '^]'.\r\r\n
****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to TI-ATEM-KASH.\r\r\nEscape character is '^]'.\r\r\n
****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n * / /____ | | | |_____ | | / / \ \ *" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...\r\r\nConnected to TI-ATEM-KASH.\r\r\nEscape character is '^]'.\r\r\n
****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n * / /____ | | | |_____ | | / / \ \ *\r\n * /_______| |_| |_______| |__| /__/ \__\ *\r\n *
*\r\n * Welcome to you !!! *\r\n * *\r\n * ( MSG5200 V2.0.3 ) *\r\n ****************************************************************\r\nLogin:" (spawn_id exp7) match glob pattern "Login:"? yes expect: set expect_out(0,string) "Login:" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "Trying 10.102.0.37...\r\r\nConnected to TI-ATEM-KASH.\r\r\nEscape character is '^]'.\r\r\n
****************************************************************\r\n * _______ _________ _______ __ __ __ __ *\r\n * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / *\r\n * / / | | | | | __ __ | \_\ \_\ /_/ *\r\n * / / | | | |_____ | |_ | |__| | _________ *\r\n * / / | | | ____| |____ ____| |_________| *\r\n * / / | | | | | | __ __ *\r\n * / /____ | | | |_____ | | / / \ \ *\r\n * /_______| |_| |_______| |__| /__/ \__\ *\r\n *
*\r\n * Welcome to you !!! *\r\n * *\r\n * ( MSG5200 V2.0.3 ) *\r\n ****************************************************************\r\nLogin:" send: sending "root\r" to { exp7 }
expect: does "" (spawn_id exp7) match glob pattern "Password:"? no
expect: does "root\r\nPassword:" (spawn_id exp7) match glob pattern "Password:"? yes expect: set expect_out(0,string) "Password:" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "root\r\nPassword:" send: sending "root\r" to { exp7 } Gate keeper glob pattern for '.*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# ' is '*# '. Activating booster.
expect: does "" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "\r\n\r\n" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "\r\n\r\nTIR-ATEM-KASH# " (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=yes re=yes expect: set expect_out(0,string) "\r\n\r\nTIR-ATEM-KASH# " expect: set expect_out(1,string) "TIR-ATEM-KASH" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "\r\n\r\nTIR-ATEM-KASH# " send: sending "show vdsl2 port 2/4/13 line-config\r" to { exp7 } Gate keeper glob pattern for '.*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# ' is '*# '. Activating booster.
expect: does "" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "s" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\n" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: " (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: 0/0(kbps) \r\ndownstream retrans-ctrl : disable \r\nupstream retrans-ctrl : " (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: 0/0(kbps) \r\ndownstream retrans-ctrl : disable \r\nupstream retrans-ctrl : disable \r\n" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: 0/0(kbps) \r\ndownstream retrans-ctrl : disable \r\nupstream retrans-ctrl : disable \r\nTIR-ATEM-KASH" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: 0/0(kbps) \r\ndownstream retrans-ctrl : disable \r\nupstream retrans-ctrl : disable \r\nTIR-ATEM-KASH# " (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=yes re=yes expect: set expect_out(0,string) "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: 0/0(kbps) \r\ndownstream retrans-ctrl : disable \r\nupstream retrans-ctrl : disable \r\nTIR-ATEM-KASH# " expect: set expect_out(1,string) "TIR-ATEM-KASH" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "show vdsl2 port 2/4/13 line-config\r\nportName : VDSL_2/4/13 \r\nportType : vdsl \r\nbaseprofile : VBASEDEF.PRF \r\nserviceprofile : VDEFVAL.PRF \r\ndpboprofile : VDPBODEF.PRF \r\nupboprofile : VUPBODEF.PRF \r\nalarmprofile : VDEFVAL.PRF \r\nActiveState : active \r\ndslLinkState : down \r\nlinktrap : inform \r\ndata-path-mode config: ptm \r\ndata-path-mode actual: ptm \r\nlastestLinkUptime : \r\nlastestLinkDowntime : \r\nxdsl2ChStatusActDataRate: 0/0(kbps) \r\ndownstream retrans-ctrl : disable \r\nupstream retrans-ctrl : disable \r\nTIR-ATEM-KASH# " send: sending "logout\r" to { exp7 } expect: read eof expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "logout\r\nConnection closed by foreign host.\r\r\n" spawn telnet TI-ATEM-KASH 1123 Trying 10.102.0.37... Connected to TI-ATEM-KASH. Escape character is '^]'.
**************************************************************** * _______ _________ _______ __ __ __ __ * * |____ / |___ ___| | _____| ____| |____ \ \ \ \ / / * * / / | | | | | __ __ | \_\ \_\ /_/ * * / / | | | |_____ | |_ | |__| | _________ * * / / | | |
____| |____ ____| |_________| * * / / | | | | | | __ __ * * / /____ | | | |_____ | | / / \ \ * * /_______| |_| |_______| |__| /__/ \__\ * *
* * Welcome to you !!! * * * * ( MSG5200 V2.0.3 ) * **************************************************************** Login:root Password:
TIR-ATEM-KASH# show vdsl2 port 2/4/13 line-config portName : VDSL_2/4/13 portType : vdsl baseprofile : VBASEDEF.PRF serviceprofile : VDEFVAL.PRF dpboprofile : VDPBODEF.PRF upboprofile : VUPBODEF.PRF alarmprofile : VDEFVAL.PRF ActiveState : active dslLinkState : down linktrap : inform data-path-mode config: ptm data-path-mode actual: ptm lastestLinkUptime : lastestLinkDowntime : xdsl2ChStatusActDataRate: 0/0(kbps) downstream retrans-ctrl : disable upstream retrans-ctrl : disable TIR-ATEM-KASH# logout Connection closed by foreign host.
注意:我无法使用expect运行脚本,但以下bash脚本效果很好
#!/bin/bash
host=$1
port=$2
user=$3
pass=$4
uport=$5
cmd1='show vlan port '${uport}
cmd2='logout'
( echo open ${host} ${port}
sleep 0.1
echo ${user}
sleep 0.1
echo ${pass}
sleep 0.1
echo ${cmd1}
sleep 0.1
echo ${cmd2}
sleep 0.1
) | telnet
答案 0 :(得分:0)
而不是给你一个“密码:”提示,你被告知“按任意键继续(Q退出)”。可能在那之后发生的事情是未知的,但如果我正在开发这个,我会假设你之后得到了密码提示,所以你想要像
这样的东西expect {
"Press any key to continue (Q to quit)" {send \r; exp_continue}
"Password:" {send "$password\r"}
}
exp_continue
命令基本上循环回到相同的expect命令,因此您仍在等待其中一种模式。
继续观察调试输出,以便了解会发生什么。