期望-中断程序-Ctrl + C [英] Expect - Interrupt program - Ctrl+C

查看:108
本文介绍了期望-中断程序-Ctrl + C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下脚本,以在远程服务器上开始捕获并随后下载文件.目前,我必须使用 Ctrl + C 暂停它,然后手动退出.

I am running the following script to start a capture on a remote server and download the file afterwards. Currently I have to pause it with Ctrl+C and manually exit.

如何替换交互并定义触发器以杀死tcpdump或捕获 Ctrl + C 并将其传递到远程服务器内部?

How can I replace the interact and define a trigger to kill the tcpdump or catch the Ctrl+C and pass it inside the 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\%"

推荐答案

发送一个 Ctrl + C ,请执行以下操作:

To send a Ctrl+C, do:

send \x03

处理传入的 Ctrl + C ,请执行以下操作:

To handle an incoming Ctrl+C, do:

trap {your handler script here} SIGINT

可能想要使处理程序脚本(可以是多行代码)将信号发送到内部进程中……

You might want to make the handler script (which can be a multi-line thing) send the signal on to the inner process…

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

但是要小心!当用户在文本模式程序中按 Ctrl + C 时(在大多数GUI中,这是一个复制操作),他们通常期望它很快消失,因此您应该注意确保信号到达后清理所有东西的时间不要太长.

But take care! When the user presses Ctrl+C in a text-mode program (in most GUIs, it's a copy action) they are usually expecting it to go away pretty soon, so you should take care to ensure that you don't spend too long after the signal arrives cleaning everything up.

这篇关于期望-中断程序-Ctrl + C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆