利用Shell开发跳板机功能脚本案例

简介:

转载地址:http://oldboy.blog.51cto.com/2561410/1915017

1)首先做好SSH密钥验证(跳板机地址192.168.33.128)
1
2
3
4
[root@oldboy~] # useradd jump  #<==要在所有机器上操作。
[root@oldboy~] # echo 123456|passwd --stdin jump #<==要在所有机器上操作。
Changingpassword  for  user jump.
passwd :all authentication tokens updated successfully.
以下操作命令仅在跳板机上操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@oldboy~] # su - jump
[jump@oldboy~]$  ssh -keygen -t dsa -P  ''  -f ~/. ssh /id_dsa  > /dev/null  2>&1   #<==生成密钥对。
[jump@oldboy~]$  ssh -copy- id  -i ~/. ssh /id_dsa .pub 192.168.33.130    #<==将公钥分发到其他服务器。
Theauthenticity of host  '192.168.33.130 (192.168.33.130)'  can't be established.
RSA keyfingerprint is fd:2c:0b:81:b0:95:c3:33:c1:45:6a:1c:16:2f:b3:9a.
Are yousure you want to  continue  connecting ( yes /no )?  yes
Warning:Permanently added  '192.168.33.130'  (RSA) to the list of known hosts.
jump@192.168.33.130'spassword:
Now trylogging into the machine, with  "ssh '192.168.33.130'" , and check  in :
   
   . ssh /authorized_keys
   
to makesure we haven 't added extra keys that you weren' t expecting.
   
[jump@oldboy~]$  ssh -copy- id  -i ~/. ssh /id_dsa .pub 192.168.33.129   #<==将公钥分发到其他服务器。
Theauthenticity of host  '192.168.33.129 (192.168.33.129)'  can't be established.
RSA keyfingerprint is fd:2c:0b:81:b0:95:c3:33:c1:45:6a:1c:16:2f:b3:9a.
Are yousure you want to  continue  connecting ( yes /no )?  yes
Warning:Permanently added  '192.168.33.129'  (RSA) to the list of known hosts.
jump@192.168.33.129'spassword:
Now trylogging into the machine, with  "ssh '192.168.33.129'" , and check  in :
   
   . ssh /authorized_keys
   
to makesure we haven 't added extra keys that you weren' t expecting.
2)实现传统的远程连接菜单选择脚本。

菜单脚本如下:




1
2
3
4
5
cat  <<menu
                   1)oldboy-192.168.33.129
                   2)oldgirl-192.168.33.130
                   3) exit
menu


3)利用linux信号防止用户中断信号在跳板机上操作。
1
2
3
functiontrapper () {
         trap  ':'  INT  EXIT TSTP TERM HUP   #<==屏蔽这些信号。
}
4)用户登录跳板机后即调用脚本(不能命令行管理跳板机),并只能按管理员的要求选单。

以下为实战内容。

脚本放在跳板机上:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@oldboy~] # echo '[ $UID -ne 0 ] && . /server/scripts/jump.sh'>/etc/profile.d/jump.sh  
[root@oldboy~] # cat /etc/profile.d/jump.sh
[ $UID- ne  0 ] && .  /server/scripts/jump .sh
[root@oldboyscripts] # cat /server/scripts/jump.sh
#!/bin/sh
#oldboy training
trapper(){
     trap  ':'  INT EXIT TSTP TERM HUP   #<==定义需要屏蔽的信号,冒号表示啥都不做。
}
main(){
while  :
do
       trapper
       clear
       cat <<menu
        1)Web01-192.168.33.129
        2)Web02-192.168.33.130
menu
read  -p "Pls input a num.:"  num
case "$num"  in
     1)
         echo  'login in 192.168.33.129.'
         ssh  192.168.33.129
         ;;
     2)
         echo  'login in 192.168.33.130.'
         ssh  192.168.33.130
         ;;
     110)
         read  -p  "your birthday:"  char
         if  "$char"  "0926" ]; then
           exit
           sleep  3
         fi
         ;;
     *)
         echo  "select error."
         esac
done
}
main

执行效果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@oldboy~] # su - jump  #<==切到普通用户即弹出菜单,工作中直接用jump登录,即弹出菜单。
      1)Web01-192.168.33.129
      2)Web02-192.168.33.130
Pls inputa num.:
      1)Web01-192.168.33.129
      2)Web02-192.168.33.130
Pls inputa num.:1   #<==选1进入Web01服务器。
login in192.168.33.129.
Lastlogin: Tue Oct 11 17:23:52 2016 from 192.168.33.128
[jump@littleboy~]$   #<==按ctrl+d退出到跳板机服务器再次弹出菜单。
      1)Web01-192.168.33.129
      2)Web02-192.168.33.130
Pls inputa num.:2      #<==选2进入Web02服务器。
login in192.168.33.130.
Lastlogin: Wed Oct 12 23:30:14 2016 from 192.168.33.128
[jump@oldgirl~]$    #<==按ctrl+d退出到跳板机服务器再次弹出菜单。
      1)Web01-192.168.33.129
     2)Web02-192.168.33.130
Pls inputa num.:110     #<==选110进入跳板机命令提示符。
yourbirthday:0926       #<==需要输入特别码才能进入的,这里管理员通道,密码要保密呦。
[root@oldboyscripts] #  #<==跳板机管理命令行。


      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1951485 ,如需转载请自行联系原作者







相关文章
|
15天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
100 1
|
18天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
15 0
|
25天前
|
存储 监控 Shell
【Shell 命令集合 磁盘管理 】Linux 关闭磁盘配额功能 quotaoff命令使用教程
【Shell 命令集合 磁盘管理 】Linux 关闭磁盘配额功能 quotaoff命令使用教程
27 1
|
25天前
|
存储 Shell Linux
【Shell 命令集合 磁盘管理 】Linux 启用指定文件系统上的磁盘配额功能 quotaon 命令使用教程
【Shell 命令集合 磁盘管理 】Linux 启用指定文件系统上的磁盘配额功能 quotaon 命令使用教程
30 1
|
4天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
135 0
|
4天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
120 0
|
15天前
|
Shell
【shell】实时查看网卡流量脚本
【shell】实时查看网卡流量脚本
|
21天前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
29 1
|
25天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 显示或设置键盘按键与其相关的功能 bind命令 使用指南
【Shell 命令集合 系统设置 】Linux 显示或设置键盘按键与其相关的功能 bind命令 使用指南
37 0
|
26天前
|
Kubernetes Shell Docker
容器服务ACK常见问题之容器服务ACK kubectl命令写到shell脚本失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。