trap实现跳板机

时间:2025-01-13 16:03:56

第一节 跳板机实现原理(图例)

trap实现跳板机

第2节 涉及到的知识点

命令:trap

拓展知识:进程与信号

trap 语法,作用,使用

[jeson@mage-jump-01 ~/]$  trap -l
   1) SIGHUP        2) SIGINT        3) SIGQUIT     4) SIGILL         5) SIGTRAP
   6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL      10) SIGUSR1

trap -l 打印所有进程的信号
trap -p 显示那些信号已经被设置了。
trap “” signals(或编号) 为空表示这个信号,执行的命令为空。可以同设置多个trap “” signals1 signals2 signals N
trap “commands” signals(编号) 收到signals指定信号时,信号功能复位同时执行commands命令。可以同时设置多个
trap signals(或编号) 没有命令部分,信号复原默认状态。可以同时恢复多个:trap signals1 signals2 signals N

使用方法:

[jeson@mage-jump- ~/]$trap 'echo "I am a boy"' INT
[jeson@mage-jump- ~/]$trap -p
  trap -- 'echo "I am a boy"' SIGINT
  trap -- '' SIGTSTP
  trap -- '' SIGTTIN
  trap -- '' SIGTTOU
[jeson@mage-jump- ~/]$^CI am a boy # 设置按下ctrl + c 的时候,执行echo "I am a boy" [jeson@mage-jump- ~/]$trap 2 # 取消 echo hello 的命令,还原组合键(ctrl + c)的默认
[jeson@mage-jump- ~/]$trap -p
  trap -- '' SIGTSTP
  trap -- '' SIGTTIN
  trap -- '' SIGTTOU
[jeson@mage-jump- ~/]$^C # 还原Ctrl+C 的默认功能
[jeson@mage-jump- ~/]$

案例:ctrl + c 后,清理工作

窗口1

窗口2

cat >touch_file.sh <<-EOF 
#!/bin/bash
trap "find /tmp -type f -name 'jeson_*' |xargs rm -f && exit " INT
while :
do
touch /tmp/jeson_\$(date +%F-%T)
sleep
done
EOF
[jeson@mage-jump-01 ~/]$ bash touch_file.sh

# 观察窗口1按下ctrl + c后,是否自动清理文件

[jeson@mage-jump-01 ~/]$ watch -n 1 ls /tmp/jeson_*

第2节 实现跳板机

跳板机主脚本:

窗口1:trap信号跳板机脚本

窗口2:设置特定用户登录不受跳板机代码影响

窗口3:测试效果

[jeson@mage-jump-01 ~/]$cat /server/scripts/tiao_ban_ji.sh

#!/bin/bash
##########################
# Date: : --
# Author: Created by Jeson
# Mail: @qq.com
# Funcsion:This scripts funcsion is tiao ban ji
##########################
function trapper(){
trap '' INT EXIT TSTP TERM HUP
}
while true
do
trapper
clear
cat <<-menu
========= Host List =======
) 10.0.0.3 lb-nginx-
) 10.0.0.4 lb-nginx-
) 10.0.0.10 web-lnmp-
) 10.0.0.11 web-lnmp-
) EXIT
menu
read -p "Please select:" num
case "$num" in
) ssh -P52113 $USER@lb-nginx-
;;
) ssh -P52113 $USER@lb-nginx-
;;
) ssh -P52113 $USER@web-lnmp-
;;
) ssh -P52113 $USER@web-lnmp-
;;
# 密码式调用分发密匙脚本
sshkey) bash /server/scripts/send_key.sh
;;
# 只对特定用户有效
) exit
esac
done

[jeson@mage-jump-01 ~/]$ cat  /etc/profile.d/tb.sh

[ $UID -ne  ] && [ $USER != 'jeson' ] &&\

source /server/scripts/tiao_ban_ji.sh

只有特定用户“root”和“jeson”可以使用“0) EXIT”功能。

======Host List========
  1) 10.0.0.3     lb-nginx-01 
  2) 10.0.0.4     lb-nginx-02 
  3) 10.0.0.10    web-lnmp-01 
  4) 10.0.0.11    web-lnmp-02
  0) EXIT
Please select:

密匙分发脚本:

密匙分发脚本1:cat /server/scripts/send_key.sh
#!/bin/bash
path_FenFa_sshKey_conf=/server/scripts/conf/sshkey.conf
path_FenFa_sshKey_exp=/server/scripts/fenfa_sshkey.exp
[ ! -f ~/.ssh/id_dsa ] && { ssh-keygen -t dsa -q -P '' -f ~/.ssh/id_dsa>/dev/null ; }
while read line
do
remote_user=$(echo $line|awk -F "[@ -]+" '{ print $1 }')
remote_password=$(echo $line|awk -F "[@ -]+" '{ print $2 }')
remote_ip=$(echo $line|awk -F "[@ -]+" '{ print $3 }')
remote_Port=$(echo $line|awk -F "[@ -]+" '{ print $4 }')
expect $path_FenFa_sshKey_exp $remote_Port $remote_user $remote_password $remote_ip ~/.ssh/id_dsa.pub &>/dev
/null
done<$path_FenFa_sshKey_conf
密匙分发exepect脚本实现非交互方式:cat /server/scripts/fenfa_sshkey.exp
#!/bin/expect
if { $argc != } {
send_user "usage: expect fenfa_sshkey.exp pub_key_file remote_user remote_passaword remote_ip\n"
exit
}
# defile var
set remote_Port [ lindex $argv ]
set remote_user [ lindex $argv ]
set remote_password [ lindex $argv ]
set remote_ip [ lindex $argv ]
set remote_dsa [ lindex $argv ]
spawn ssh-copy-id -i $remote_dsa -p $remote_Port $remote_user@$remote_ip
expect {
"yes/no" { send "yes\r";exp_continue }
"*password" { send "$remote_password\r";exp_continue }
}
 需要分发到的主机配置文件:
[root@web-lnmp-01 ~/]#cat /server/scripts/conf/sshkey.conf 
root  10.0.0.31
jeson 10.0.0.200
jeson 10.0.0.10