美文网首页
信号控制 & expect

信号控制 & expect

作者: 慕知 | 来源:发表于2021-01-05 10:24 被阅读0次

一,信号说明

# 查看所有信号
root@m01~]# kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
... ...

常用:
 1) HUP   挂起;让进程重新加载配置
 2) INT  中断;ctrl+c 产生的信号,通知前台进程终止进程
 3) QUIT    ctrl-\产生的信号
 9) KILL     强制退出
20) TSTP   停止进行运行;ctrl+z产生的信号



HUP示例:
[root@m01~]# yum install -y vsftpd
[root@m01/var/ftp]# touch 123 > 1.txt

[root@m01/var/ftp]# setenforce 0
setenforce: SELinux is disabled
[root@m01/var/ftp]# systemctl stop firewalld.service
[root@m01/var/ftp]# systemctl start vsftpd

#window电脑可测试访问 ftp://10.0.0.61
[root@m01/var/ftp]# ps -aux | grep [v]sftpd

默认以下为共享目录
drwxr-xr-x 2 root root 6 2020-10-14 00:10 pub

#在进程中改变共享目录
[root@m01/var/ftp]# mkdir /test
[root@m01/var/ftp]# echo 22222 > /test/2.txt
#最后一行添加
[root@m01/var/ftp]# vim /etc/vsftpd/vsftpd.conf
anon_root=/test

#执行信号命令
[root@m01/var/ftp]# kill -1 pid号


刷新页面即可发现共享目录换了

[root@m01/var/ftp]# ps -aux | grep [v]sftpd
依然是同一个pid号

二,信号处理

  • 捕捉信号并处理
方式一:捕捉信号,不执行任何操作
[root@m01~]# vim /script/signal.sh
#!/bin/bash
trap "" INT TSTP
echo $$
sleep 100

# 执行查看ctrl+c,ctrl+z没有反应
[root@m01~]# /script/signal.sh
55043
^C^C^C^


方式二:捕捉信号,执行引号内的操作
[root@m01~]# vim /script/signal.sh
#!/bin/bash
trap "echo 不能使用ctrl+c" INT
echo $$
sleep 100


[root@m01~]# /script/signal.sh
55071
^C不能使用ctrl+c



方式三:同时还 捕捉多个信号
trap "" INT TSTP QUIT HUP

注意⚠️: 
捕捉信号需要放在脚本的前面

三,hup信号

---->如何让一个进程脱离当前窗口运行

问题:  在一个窗口执行脚本或者命令,突然关闭窗口,会导致任务全部消息,以下为例

[root@m01~]# ping www.baidu.com &>/dev/null &
[1] 55173

方式一:脱离当前窗口
[root@m01~]# exit

# 原理是退出父进程即 bash进程,就会被systemd接管
可通过以下命令查看
[root@m01~]# ps -elf | grep [p]ing
4 S root      55204  55187  0  80   0 - 37492 poll_s 13:16 pts/1    00:00:00 ping www.baidu.com





方式二:加上nohup
[root@m01~]# nohup ping www.baidu.com &> /dev/null &




方式三:加上setsid
[root@m01~]# setsid  ping www.baidu.com &> /dev/null &






方式四:
[root@m01~]# (ping www.baidu.com &> /dev/null &)





方式五:
[root@m01~]# yum install -y screen
[root@m01~]# screen -S zzz        # zzz是打开新的窗口的名字  

四, expect 编程工具

  • 把shell中脚本中的交互式操作变成非交互
[root@m01~]# yum install -y expect


[root@m01~]# vim /script/expect.sh
#!/usr/bin/expect
spawn ssh root@10.0.0.7 uptime
expect (yes/no)
send "yes\n"

expect "password"
send "1\n"
expect eof


[root@m01~]# /script/expect.sh
spawn ssh root@10.0.0.7 uptime
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:C6IdPkkcDJg9rqYo2BkdI/Z01F4SOuwKzJCDDCqh3nI.
ECDSA key fingerprint is MD5:22:2c:97:57:a5:15:af:5e:76:38:de:46:3a:28:e7:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
 15:47:39 up 2 days, 16:23,  3 users,  load average: 3.00, 3.01, 3.05




#!/usr/bin/expect
# 先下载expect,用expect解析spawn

spawn ssh root@10.0.0.7 uptime
# spawn是用来交互式的命令,uptime是连接后执行的命令

expect (yes/no)
# expect 从进程接收字符

send "yes\n"
# send 向进程发送命令

expect "password"
# 交互式环境中,出现password也可“*ssword”,只要包含能识别出即可

send "1\n"
输入后要换行  \n代表换行

expect eof
# 语法 不可缺少



==========

以上脚本有弊端,只能执行第一次,再次执行会出现以下:
[root@m01~]# /script/expect.sh
spawn ssh root@10.0.0.7 uptime
root@10.0.0.7's password:



修改脚本如下便能继续执行,但是不能在第一次执行成功

#!/usr/bin/expect
spawn ssh root@10.0.0.7 uptime

expect "password"
send "1\n"
expect eof
  • expect一问一答,所以标准脚本如下
[root@m01~]# vim /script/expect.sh
!/usr/bin/expect
spawn ssh root@10.0.0.7 uptime
set timeout 2
expect {
"yes/no" {send "yes\n";exp_continue}
"passwo*" {send "1\n"}
}
expect eof




多次执行都可以
[root@m01~]# /script/expect.sh
spawn ssh root@10.0.0.7 uptime
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:C6IdPkkcDJg9rqYo2BkdI/Z01F4SOuwKzJCDDCqh3nI.
ECDSA key fingerprint is MD5:22:2c:97:57:a5:15:af:5e:76:38:de:46:3a:28:e7:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
 16:18:59 up 2 days, 16:55,  3 users,  load average: 3.17, 3.12, 3.08





!/usr/bin/expect
spawn ssh root@10.0.0.7 uptime
set timeout 2
# 设置超时时间,可不设置
expect {
"yes/no" {send "yes\n";exp_continue}
"passwo*" {send "1\n"}
}
# exp_continue如果第一条没匹配上,继续匹配后面的
expect eof

  • expect脚本补充
可以先连接上主机,一条一条执行命令

[root@m01~]# vim /script/expect1.sh
#!/usr/bin/expect
spawn ssh root@10.0.0.7
#set timeout 2
expect {
"yes/no" {send "yes\n";exp_continue}
"passwo*" {send "1\n"}
}
expect "#"
send "uptime\n"
expect "#"
send "pwd\n"
expect "exit"
expect eof
# 不要忘记expect "exit" 退出,不然一直停在原地


[root@m01~]# /script/expect1.sh
spawn ssh root@10.0.0.7
root@10.0.0.7's password:
Last login: Fri Dec 18 20:11:39 2020 from 172.16.1.61
[root@\ web01~]# uptime
 10:01:30 up 2 days, 19:47,  4 users,  load average: 3.06, 3.05, 3.05
[root@\ web01~]# pwd
/root
  • expect运用在shell脚本中
方式一:
[root@m01/script]# vim expect2.sh
#!/usr/bin/expect

set user "root"
set ip "10.0.0.7"
set passwd "1"
set cmd "uptime"
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes";exp_continue}
"passwo*" {send "$passwd\n"}
}
expect eof


执行:
[root@m01/script]# ./expect2.sh
spawn ssh root@10.0.0.7 uptime
root@10.0.0.7's password:
 10:16:15 up 2 days, 20:02,  3 users,  load average: 3.01, 3.04, 3.05






方式二:  (推荐)
[root@m01/script]# vim expect3.sh
#!/bin/bash

user="root"
ip="10.0.0.7"
cmd="uptime"
passwd="1"

expect << EOF
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes";exp_continue}
"passw*" {send $passwd\n}
}
expect eof
EOF




执行:
[root@m01/script]# ./expect3.sh
spawn ssh root@10.0.0.7 uptime
root@10.0.0.7's password:
 10:20:30 up 2 days, 20:06,  3 users,  load average: 3.03, 3.04, 3.05

相关文章

网友评论

      本文标题:信号控制 & expect

      本文链接:https://www.haomeiwen.com/subject/xfehoktx.html