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

第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

trap实现跳板机的更多相关文章

  1. linux跳板机开发之trap信号机应用

    场景1:公司新招聘了一个配置管理员,他的工作是负责将公司开发人员写的新代码依次分发到办公室测试环境.IDC测试环境和正式线上环境.因此公司需要开发一个程序,当配置管理员登录服务器,只能进入分发的管理界 ...

  2. Shell实现跳板机,为什么用跳板机

    整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1.     问题 ...

  3. shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机

    1.NGINX启动脚本 #!/bin/bash # chkconfig: 235 32 62 # description: nginx [ -f /etc/init.d/functions ] &am ...

  4. 运维常用shell脚本一(系统指标巡检、自动创建用户、跳板机)

    一.系统指标巡检脚本 #!/bin/bash menu(){ cat <<EOF +---------------------------------------------+ | 日常巡 ...

  5. 13、ssh跳板机

    13.1.前提条件: 1.跳板机服务器和其它服务器建立了ssh秘钥登录: 2.目前的环境: (1)各服务器上都建立了lc用户,并给于sudo (lc ALL= NOPASSWD:ALL )权限,lc就 ...

  6. 【自动部署】Ansible 怎么通过堡垒机/跳板机 访问目标机器

    Ansible机器的 /root/.ssh/config 配置如下即可:Host 目标机器IP User root IdentityFile=/root/.ssh/xxx_id_rsa ProxyCo ...

  7. navicat使用跳板机连接数据库-ssh

    1. 目标数据库的域名/IP,端口,用户名,密码:如图1 2. 这时候不要点OK!选择SSH这个tab 3. 选中User SSH Tunnel:填写跳板机域名/IP,用户名,密码(注意:端口22不要 ...

  8. 跨越跳板机传文件nc

    从线上服务器与本机互传文件 传输方 nc -l 10000 < a.tar 接收方 nc xx.xx.xx.xx 10000 >a.tar 原理: 文件传输方运行nc,指定端口,设置监听文 ...

  9. 在有跳板机的情况下,SecureCRT自动连接到目标服务器

    为了服务器的安全,运维人员经常会要求我们先登录到跳板机,然后再SSH连接到目标服务器.但是这样是很繁琐的,每次在SecureCRT创建一个连接,都需要输入SSH命令,然后输入密码. 下面的方法可以实现 ...

随机推荐

  1. PTA——完全数

    PTA 7-45 找完数 网友“云上明月”的程序: #include<stdio.h> int isPerfect(int num); int main() { ; int maxFact ...

  2. best practices for designing web api

    restful why: meaningful This will be improve efficiency , less documents , just read the code auto g ...

  3. vue学习笔记——组件的优化

    Vue 应用性能优化指南 1 给组件定义name,然后在同级目录新建index文件: import Count from './count.vue' export Count; 2 优化大数据的列表 ...

  4. Nginx网站实现ssl安全套接字

    nginx.conf配置 server { listen 443 ssl; server_name www.example.com; ssl on; ssl_certificate cert.pem; ...

  5. Unity外包团队:Daydream控制器只提供了3个自由度

    HTC Vive,Oculus Rift以及微软即将推出的MR头显都拥有6自由度的运动控制器,这意味着你在虚拟世界中可以任意摆动你的手.然而,Daydream控制器只提供了3个自由度,这对于手部运动具 ...

  6. 【Python】2.x与3​​.x版本的选用&版本间的区别

    转自 http://www.runoob.com/python/python-2x-3x.html 一.2.x与3​​.x版本的选用建议 Python的3​​.0版本,常被称为Python 3000, ...

  7. STM32定时器时间的计算方法

    本文出自:https://wenku.baidu.com/view/e3bdfb7601f69e31433294c4.htmlSTM32定时器时间的计算方法STM32中的定时器有很多用法:(一)系统时 ...

  8. Windows 下最佳的 C++ 开发的 IDE 是什么?

    作者:渡世白玉链接:https://www.zhihu.com/question/19589089/answer/30312199来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  9. Android之MainActivity类

    一.MainActivity: 1.每个种语言都有一个程序入库(如:C#main函数),而Android程序的入口就是Mani Actiivty函数. 2.Activity是Android的核心类(a ...

  10. git之sourceTree操作流程

    1x.sourceTree的使用流程  12.Git管理工具对比(GitBash.EGit.SourceTree)  11.SourceTree使用SSH克隆码云项目 ====== 1x.source ...