linux - Exit handler in expect script not removing file -
i'm having trouble expect script writing. goal of script ssh partner , obtain network configuration information. information saved in file , copied local host processing. file removed remote host upon exit.
under normal conditions, works great , expect. however, should user press ^c interrupt script during scp process, file not removed remote host. left there. not sure why or how force removal. when stepping through signal handler , exit handler during such case, appears reason exit handler doesn't think file exists , skips if statement. have no idea why, though.
i have copied exit handler , signal handler script below. please let me know if have ideas or see obvious errors. i'm not sure go here.
# configure exit handler exit -onexit { # remove configuration info file if exists if {[file exists ptinit.txt]} { send "rm -rf ptinit.txt\r" expect -exact "rm -rf ptinit.txt\r" } } # configure signal trap remove partner file before exiting proc errsig_handler {pidlst} { send_user "\nstop command received user. exiting.\n" {set [expr [llength $pidlst]-1]} {$i >= 0} {incr -1} { send_user "current pid is: [lindex $pidlst $i]\n" # if pid not null , process running, kill if {[lindex $pidlst $i] != "" && [exec kill -0 [lindex $pidlst $i] 2>/dev/null] == ""} { send_user "pid [lindex $pidlst $i] not null , running.\n" exec kill -9 [lindex $pidlst $i] } } } trap {errsig_handler $cur_pid} {int tstp} update: tried method suggested dinesh, still having issues. updated exit handler code follows: exit -onexit { exp_internal 1 unset expect_out(buffer)
# remove configuration info file 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 not successful - ptinit.txt needs deleted manually." } -re $prompt { sleep 5 send "exit\r" expect eof {puts "eof received."; sleep 2} } } send_user "leaving exit handler\n" }
the way work commenting out signal handler loop kills spawned pids. when uncommented, exit handler expect times out.
either way, file still not removed remote system. see exp_internal 1:
stop command received user. exiting. in exit handler send: sending "rm -rf ptinit.txt\r" { exp9 } gate keeper glob pattern '(%|#|>|\$) $' ''. not usable, disabling performance booster. expect: " \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: " \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" { 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 update 2:
i updated exit handler following:
exit -onexit { exp_internal 1 unset expect_out(buffer) # remove configuration info file remote server set filename ptinit.txt send_user "\nin exit handler\n" send "rm -rf $filename\r" expect { "cannot remove" { puts "file deletion not successful" } -re $prompt { puts "file deleted" } } send_user "leaving exit handler\n" } this debugging information - still no file deleted.
stop command received user. exiting. in exit handler send: sending "rm -rf ptinit.txt\r" { exp9 } gate keeper glob pattern '(%|#|>|\$) $' ''. not usable, disabling performance booster. expect: " \r\n" (spawn_id exp9) match glob pattern "cannot remove"? no "(%|#|>|\$) $"? (no gate, re only) gate=yes re=no expect: " \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: " \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: " \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
the problem because of file exists. checks file path present or not in local machine wherever running expect script, not in remote directory.
removing solve problem.
#this common approach few known prompts #if device's prompt missing here, can add same. set prompt "#|>|\\\$"; # escaped `$` symbol backslash match literal '$' set filename ptinit.txt send "rm -rf $filename\r" expect { "cannot remove" { puts "file deletion not successful"} -re $prompt { puts "file deleted" } } update : in code, expecting rm -rf ptinit.txt\r wrong. because expect see sent spawned process (that rm -rf ptinit.txt\r) , match that. due this, never sent spawned process.
so, can enable debug above code given me , share here?
Comments
Post a Comment