我在编写期望脚本时遇到了一些问题。该脚本的目标是通过 SSH 连接到合作伙伴并获取网络配置信息。该信息保存在文件中并复制回本地主机进行处理。然后在退出时从远程主机中删除该文件。
在正常情况下,一切正常,正如我所料。但是,如果用户在 SCP 过程中按 ^C 来中断脚本,则文件不会从远程主机中删除。它留在那里。我不确定为什么会这样或如何强制将其删除。在这种情况下单步执行我的信号处理程序和退出处理程序时,似乎出于某种原因退出处理程序认为文件不存在并跳过 if 语句。不过,我不知道为什么。
我从下面的脚本中复制了退出处理程序和信号处理程序。如果您有任何想法或发现任何明显的错误,请告诉我。我不知道从这里去哪里。
# Configure exit handler
exit -onexit {
# Remove configuration info file if it exists
if {[file exists ptinit.txt]} {
send "rm -rf ptinit.txt\r"
expect -exact "rm -rf ptinit.txt\r"
}
}
# Configure signal trap to remove partner file before exiting
proc errsig_handler {pidlst} {
send_user "\nStop command received from user. Exiting.\n"
for {set i [expr [llength $pidlst]-1]} {$i >= 0} {incr i -1} {
send_user "Current PID is: [lindex $pidlst $i]\n"
# If pid is not null and process is currently running, kill it
if {[lindex $pidlst $i] != "" && [exec kill -0 [lindex $pidlst $i] 2>/dev/null] == ""} {
send_user "PID [lindex $pidlst $i] is not null and is currently running.\n"
exec kill -9 [lindex $pidlst $i]
}
}
}
trap {errsig_handler $cur_pid} {INT TSTP}
更新: 我尝试了 Dinesh 建议的方法,但仍然有问题。我更新了退出处理程序代码如下:exit -onexit { exp_internal 1 unset expect_out(buffer)
# Remove configuration info file from remote server
send_user "\nIN EXIT HANDLER\n"
send "rm -rf ptinit.txt\r"
expect {
"rm -rf ptinit.txt\r" {
sleep 5
send "exit\r"
expect eof {puts "EOF in rm match received."; sleep 2}
}
"cannot remove" {
puts "File deletion was not successful - ptinit.txt needs to be deleted manually."
}
-re $prompt {
sleep 5
send "exit\r"
expect eof {puts "EOF received."; sleep 2}
}
}
send_user "LEAVING EXIT HANDLER\n"
}
我可以让它工作的唯一方法是注释掉我的信号处理程序以杀死产生的 PID。取消注释时,退出处理程序期望超时。
无论哪种方式,该文件仍未从远程系统中删除。这是我从 exp_internal 1 看到的:
Stop command received from user. Exiting.
IN EXIT HANDLER
send: sending "rm -rf ptinit.txt\r" to { exp9 }
Gate keeper glob pattern for '(%|#|>|\$) $' is ''. Not usable, disabling the performance booster.
expect: does " \r\n" (spawn_id exp9) match glob pattern "rm -rf ptinit.txt\r"? no
"cannot remove"? no
"(%|#|>|\$) $"? (No Gate, RE only) gate=yes re=no
expect: does " \r\nrm -rf ptinit.txt\r\n" (spawn_id exp9) match glob pattern "rm -rf ptinit.txt\r"? yes
expect: set expect_out(0,string) "rm -rf ptinit.txt\r"
expect: set expect_out(spawn_id) "exp9"
expect: set expect_out(buffer) " \r\nrm -rf ptinit.txt\r"
send: sending "exit\r" to { exp9 }
expect: read eof
expect: set expect_out(spawn_id) "exp9"
expect: set expect_out(buffer) "\n\rptinit.txt 0% 0 0.0KB/s --:-- ETA\rptinit.txt
100% 35 0.0KB/s 00:00 \r\nexit\r\n"
EOF in rm match received.
LEAVING EXIT HANDLER
tty_set: raw = 5, echo = 0
更新 2:
我将退出处理程序更新为以下内容:
exit -onexit {
exp_internal 1
unset expect_out(buffer)
# Remove configuration info file from remote server
set filename ptinit.txt
send_user "\nIN EXIT HANDLER\n"
send "rm -rf $filename\r"
expect {
"cannot remove" { puts "File deletion is not successful" }
-re $prompt { puts "File deleted" }
}
send_user "LEAVING EXIT HANDLER\n"
}
这是调试信息 - 仍然没有删除文件。
Stop command received from user. Exiting.
IN EXIT HANDLER
send: sending "rm -rf ptinit.txt\r" to { exp9 }
Gate keeper glob pattern for '(%|#|>|\$) $' is ''. Not usable, disabling the performance booster.
expect: does " \r\n" (spawn_id exp9) match glob pattern "cannot remove"? no
"(%|#|>|\$) $"? (No Gate, RE only) gate=yes re=no
expect: does " \r\nrm -rf ptinit.txt\r\n" (spawn_id exp9) match glob pattern "cannot remove"? no
"(%|#|>|\$) $"? (No Gate, RE only) gate=yes re=no
expect: does " \r\nrm -rf ptinit.txt\r\n\rptinit.txt 0% 0 0.0KB/s --:-- ETA" (spawn_id
exp9) match glob pattern "cannot remove"? no
"(%|#|>|\$) $"? (No Gate, RE only) gate=yes re=no
expect: does " \r\nrm -rf ptinit.txt\r\n\rptinit.txt 0% 0 0.0KB/s --:-- ETA\rptinit.tx
t 100% 35 0.0KB/s 00:00 \r\n" (spawn_id exp9) match glob pattern "cannot remove"? no
"(%|#|>|\$) $"? (No Gate, RE only) gate=yes re=no
expect: read eof
expect: set expect_out(spawn_id) "exp9"
expect: set expect_out(buffer) " \r\nrm -rf ptinit.txt\r\n\rptinit.txt 0% 0 0.0KB/s --
:-- ETA\rptinit.txt 100% 35 0.0KB/s 00:00 \r\n"
LEAVING EXIT HANDLER
tty_set: raw = 5, echo = 0