我正在尝试运行一个调用期望的 bash 脚本作为守护进程,只要脚本仍然附加到终端,一切都很好。但是当我将脚本移动到后台或从 crontab 执行它时,生成的 telnet 进程不再与预期脚本通信,调试时出现超时错误,并且脚本失败。
另一件值得一提的事情是,我仅在某些具有较旧固件版本的设备上遇到此问题,(升级成本高昂且不是一种选择)在较新的设备上可以完美运行相同的脚本。
有关如何解决此问题的任何反馈将不胜感激。
调试输出如下:
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...rrn" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to KASH.rrnEscape character is '^]'.rrn" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to KASH.rrnEscape character is '^]'.rrn ****************************************************************rn * _______ _________ _______ __ __ __ __ *" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to KASH.rrnEscape character is '^]'.rrn ****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn * / /____ | | | |_____ | | / / *" (spawn_id exp6) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to KASH.rrnEscape character is '^]'.rrn ****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn * / /____ | | | |_____ | | / / *rn * /_______| |_| |_______| |__| /__/ __ *rn * *rn * Welcome to you !!! *rn * *rn * ( MSG5200 V2.0.3 ) *rn ****************************************************************rnLogin:" (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...rrnConnected to KASH.rrnEscape character is '^]'.rrn ****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn * / /____ | | | |_____ | | / / *rn * /_______| |_| |_______| |__| /__/ __ *rn * *rn * Welcome to you !!! *rn * *rn * ( MSG5200 V2.0.3 ) *rn ****************************************************************rnLogin:"
send: sending "rootr" 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 "rootr" 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-configr" 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 "logoutr" 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...rrn" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to TI-ATEM-KASH.rrnEscape character is '^]'.rrn" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to TI-ATEM-KASH.rrnEscape character is '^]'.rrn
****************************************************************rn * _______ _________ _______ __ __ __ __ *" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to TI-ATEM-KASH.rrnEscape character is '^]'.rrn
****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to TI-ATEM-KASH.rrnEscape character is '^]'.rrn
****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn * / /____ | | | |_____ | | / / *" (spawn_id exp7) match glob pattern "Login:"? no
expect: does "Trying 10.102.0.37...rrnConnected to TI-ATEM-KASH.rrnEscape character is '^]'.rrn
****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn * / /____ | | | |_____ | | / / *rn * /_______| |_| |_______| |__| /__/ __ *rn *
*rn * Welcome to you !!! *rn * *rn * ( MSG5200 V2.0.3 ) *rn ****************************************************************rnLogin:" (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...rrnConnected to TI-ATEM-KASH.rrnEscape character is '^]'.rrn
****************************************************************rn * _______ _________ _______ __ __ __ __ *rn * |____ / |___ ___| | _____| ____| |____ / / *rn * / / | | | | | __ __ | _ _ /_/ *rn * / / | | | |_____ | |_ | |__| | _________ *rn * / / | | | ____| |____ ____| |_________| *rn * / / | | | | | | __ __ *rn * / /____ | | | |_____ | | / / *rn * /_______| |_| |_______| |__| /__/ __ *rn *
*rn * Welcome to you !!! *rn * *rn * ( MSG5200 V2.0.3 ) *rn ****************************************************************rnLogin:" send: sending "rootr" to { exp7 }
expect: does "" (spawn_id exp7) match glob pattern "Password:"? no
expect: does "rootrnPassword:" (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) "rootrnPassword:" send: sending "rootr" 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 "rnrn" (spawn_id exp7) match regular expression ".*[^-A-Za-z0-9_]([-A-Za-z0-9_]*)# "? Gate "*# "? gate=no
expect: does "rnrnTIR-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) "rnrnTIR-ATEM-KASH# " expect: set expect_out(1,string) "TIR-ATEM-KASH" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "rnrnTIR-ATEM-KASH# " send: sending "show vdsl2 port 2/4/13 line-configr" 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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rn" (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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: " (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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: 0/0(kbps) rndownstream retrans-ctrl : disable rnupstream 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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: 0/0(kbps) rndownstream retrans-ctrl : disable rnupstream retrans-ctrl : disable rn" (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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: 0/0(kbps) rndownstream retrans-ctrl : disable rnupstream retrans-ctrl : disable rnTIR-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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: 0/0(kbps) rndownstream retrans-ctrl : disable rnupstream retrans-ctrl : disable rnTIR-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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: 0/0(kbps) rndownstream retrans-ctrl : disable rnupstream retrans-ctrl : disable rnTIR-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-configrnportName : VDSL_2/4/13 rnportType : vdsl rnbaseprofile : VBASEDEF.PRF rnserviceprofile : VDEFVAL.PRF rndpboprofile : VDPBODEF.PRF rnupboprofile : VUPBODEF.PRF rnalarmprofile : VDEFVAL.PRF rnActiveState : active rndslLinkState : down rnlinktrap : inform rndata-path-mode config: ptm rndata-path-mode actual: ptm rnlastestLinkUptime : rnlastestLinkDowntime : rnxdsl2ChStatusActDataRate: 0/0(kbps) rndownstream retrans-ctrl : disable rnupstream retrans-ctrl : disable rnTIR-ATEM-KASH# " send: sending "logoutr" to { exp7 } expect: read eof expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "logoutrnConnection closed by foreign host.rrn" 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.
注意:我无法使用期望运行脚本,但以下 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
不是给你一个"密码:"提示,而是告诉你"按任意键继续(Q退出)"。可能之后会发生什么是未知的,但是如果我正在开发这个,我会假设您之后会收到密码提示,所以您想要类似的东西
expect {
"Press any key to continue (Q to quit)" {send r; exp_continue}
"Password:" {send "$passwordr"}
}
exp_continue
命令基本上循环回相同的期望命令,因此您仍在等待其中一种模式。
继续观察调试输出,以便查看发生的情况。