Expect - interrupt program - Ctrl + C

I run the following script to start the capture on a remote server and upload the file later. Currently, I have to pause it with Ctrl+ Cand exit manually.

How can I replace the interaction and define a trigger to kill tcpdump or catch Ctrl+ Cand send it to a remote server?

spawn ssh "$user_ssh\@$ssh_server"

expect {
        "*password"     { send "$pass\n";   exp_continue}
        "root\@*"       { }
        timeout  { puts "time out expecting password or bash"; exit 1 }
    }

send "sudo tcpdump -i $intf -s0 -w $file -v\n";
interact

spawn scp "$user_ssh\@$ssh_server:$file" .

expect "password:"
send "$pass_ssh\n";
expect "100\%"
+4
source share
1 answer

To send a Ctrl+ C, do:

send \x03

To process incoming Ctrl+ C, do:

trap {your handler script here} SIGINT

, script ( ) ...

trap {
    send \x03
    send_user "You pressed Ctrl+C\n"
} SIGINT

! Ctrl + C ( ), , , , , .

+5

Source: https://habr.com/ru/post/1541817/


All Articles