美文网首页
linux使用shell脚本实现跳板机服务

linux使用shell脚本实现跳板机服务

作者: 南南宫问天 | 来源:发表于2020-04-09 20:27 被阅读0次
[root@db scripts]# cat jumpserver.sh 
#!/bin/bash
vlan=172.16.210. ##网段变量
memu(){  ##定义网段的变量
cat <<EOF
####################################################################################
                 1.master 172.16.210.11
                 2.node1 172.16.210.12
                 3.node2 172.16.210.13
                 4.controller 172.16.210.10
####################################################################################
EOF
}
memu

read -p "请输入你要连接的服务器编号: " num
case $num in
    1)
    ssh root@${vlan}11 ;;
    2)
    ssh root@${vlan}12 ;;
    3)
    ssh root@${vlan}13 ;;
    4)
    ssh root@${vlan}10 ;;
    *)
     echo "请输入你要连接的服务器编号: "
esac

跳板机提前做好免密

只需要输入编号就可以成功连接各主机了

image.png
image.png

如果生产环境的跳板机有高安全性,可改良下脚本,使其跳板机只能作能做跳板机,不能退出脚本,只能连接脚本指定的主机

[root@db scripts]# cat jumpserver.sh 
#!/bin/bash
wd=172.16.210.
memu(){
cat <<EOF
####################################################################################
                 1.master 172.16.210.11
                 2.node1 172.16.210.12
                 3.node2 172.16.210.13
                 4.controller 172.16.210.10
####################################################################################
EOF
}
memu
trap "" INT TSTP HUP  ##设置隔绝信号 使其脚本具有不被打断性
while true  ##设置死循环
do
read -p "请输入你要连接的服务器编号: " num
case $num in
    1)
    ssh root@${wd}11 ;;
    2)
    ssh root@${wd}12 ;;
    3)
    ssh root@${wd}13 ;;
    4)
    ssh root@${wd}10 ;;
    *)
     echo "请输入你要连接的服务器编号: "
esac
done
[root@db scripts]# echo "sh /server/scripts/jumpserver.sh" >> /etc/profile  ##设置开启新终端连接跳板机也会执行这个脚本

测试:

[root@db scripts]# sh jumpserver.sh 
####################################################################################
                 1.master 172.16.210.11
                 2.node1 172.16.210.12
                 3.node2 172.16.210.13
                 4.controller 172.16.210.10
####################################################################################
请输入你要连接的服务器编号: ^C^C^C^C^C  #ctrl+c无效
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 请输入你要连接的服务器编号:  #ctrl+d无效
请输入你要连接的服务器编号: 请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: exit ##exit无效
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: quit ##quit无效
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 2
Last login: Thu Apr  9 20:12:01 2020 from 172.16.210.250
Connection to 172.16.210.12 closed.
请输入你要连接的服务器编号: 
[root@controller ~]# ssh root@172.16.210.36 ##从新终端尝试连接跳板机
The authenticity of host '172.16.210.36 (172.16.210.36)' can't be established.
ECDSA key fingerprint is e0:cc:fc:24:a3:f9:70:bd:14:cf:78:37:bc:5a:2f:cb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.210.36' (ECDSA) to the list of known hosts.
root@172.16.210.36's password: 
Last login: Thu Apr  9 20:23:36 2020 from 172.16.210.250
当前的主机名是: db
当前系统的版本是:  CentOS Linux 7 (Core)
当前系统的内核是: Linux 3.10.0-693.el7.x86_64
当前系统的虚拟平台是: vmware
当前ip地址是: fe80::250:56ff:febf:8ab0%ens192 172.16.210.36
当前lo地址是: 127.0.0.1
当前外网地址是: 117.141.205.10
####################################################################################
                 1.master 172.16.210.11
                 2.node1 172.16.210.12
                 3.node2 172.16.210.13
                 4.controller 172.16.210.10
####################################################################################
请输入你要连接的服务器编号:   ##一连接跳板机就执行这个脚本,做到真正跳板机服务

如果有天想解除跳板机服务,则需要按照以下步骤(前提必须要有root密码)
1.找一台普通服务器

[root@node2-13 ~]# ssh-keygen ##生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:AngHsaXgAZa+DwW/z83GJ2Mel1eBg5gjo8F1bl0hB8Q root@node2-13
The key's randomart image is:
+---[RSA 2048]----+
|.o+ oo..o+.+.    |
|.+.+.=o +E= .    |
|. =o=o.* o o .   |
| . +oo+ .   . .  |
|  o.. . S    .   |
| o .   .  . .    |
|  o o +. o .     |
|   . o Oo..      |
|      +.+        |
+----[SHA256]-----+

[root@node2-13 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.210.36  ##把共钥上传给跳板机
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.210.36's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.16.210.36'"
and check to make sure that only the key(s) you wanted were added.

我们不登陆直接在普通服务器查看跳板机的进程

[root@node2-13 ~]# ssh 172.16.210.36 "ps auxf" | tail -20
dbus       765  0.0  0.0  24596  1832 ?        Ss   18:31   0:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root       768  0.6  0.1 224632  4888 ?        Ssl  18:31   0:53 /usr/sbin/rsyslogd -n
root       770  0.1  0.0 126288  1600 ?        Ss   18:31   0:14 /usr/sbin/crond -n
root       782  0.0  0.7 336168 28728 ?        Ssl  18:31   0:02 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root       785  0.0  0.0 110096   848 tty1     Ss+  18:31   0:00 /sbin/agetty --noclear tty1 linux
root       787  0.8  0.2 471884  8852 ?        Ssl  18:31   1:09 /usr/sbin/NetworkManager --no-daemon
root      1083  0.0  0.1 106048  4140 ?        Ss   18:31   0:03 /usr/sbin/sshd -D
root      5176  0.0  0.1 145748  5360 ?        Ss   20:08   0:00  \_ sshd: root@pts/1
root      5178  0.1  0.0 116160  2856 pts/1    Ss   20:08   0:02  |   \_ -bash
root      5604  0.0  0.0 113192  1384 pts/1    S+   20:43   0:00  |       \_ sh /server/scripts/jumpserver.sh
root      5643  1.0  0.1 145748  5280 ?        Ss   20:44   0:00  \_ sshd: root@notty
root      5645  0.0  0.0 151412  2112 ?        Rs   20:44   0:00      \_ ps auxf
root      1084  0.5  0.4 562500 18628 ?        Ssl  18:31   0:45 /usr/bin/python -Es /usr/sbin/tuned -l -P
root      1429  0.0  0.0 120880  2248 ?        Ss   18:34   0:00 nginx: master process /usr/sbin/nginx
nginx     1430  0.0  0.0 121276  3324 ?        S    18:34   0:00  \_ nginx: worker process
nginx     1431  0.0  0.0 121276  3324 ?        S    18:34   0:00  \_ nginx: worker process
nginx     1432  0.0  0.0 121276  3324 ?        S    18:34   0:00  \_ nginx: worker process
nginx     1433  0.0  0.0 121276  3120 ?        S    18:34   0:00  \_ nginx: worker process
root      2602  0.7  0.0  37232  2884 ?        Ss   18:55   0:47 /usr/lib/systemd/systemd-journald
root      5250  0.0  0.1 176104  4600 ?        S    20:11   0:00 ssh root@172.16.210.11

找到脚本进程pid然后直接kill掉

root      5604  0.0  0.0 113192  1384 pts/1    S+   20:43   0:00  |       \_ sh /server/scripts/jumpserver.sh
[root@node2-13 ~]# ssh 172.16.210.36 "kill -9 5604" ##杀死脚本进程 

这时候已经连接的窗口就可以恢复使用


这样的解决方法难免有些繁琐

我们可以在脚本一开始的设置一个vip,当脚步检测到vip就会结束脚本,一般这个vip只告诉公司里的运维

[root@db scripts]# cat jumpserver.sh 
#!/bin/bash
wd=172.16.210.
memu(){
cat <<EOF
####################################################################################
                 1.master 172.16.210.11
                 2.node1 172.16.210.12
                 3.node2 172.16.210.13
                 4.controller 172.16.210.10
####################################################################################
EOF
}
memu
trap "" INT TSTP HUP
while true
do
read -p "请输入你要连接的服务器编号: " num
case $num in
    1)
    ssh root@${wd}11 ;;
    2)
    ssh root@${wd}12 ;;
    3)
    ssh root@${wd}13 ;;
    4)
    ssh root@${wd}10 ;;
    vip) exit ;; ##当检测到用户输入的是vip时,退出脚本
    *)
     echo "请输入你要连接的服务器编号: "
esac
done
[root@db scripts]# sh jumpserver.sh 
####################################################################################
                 1.master 172.16.210.11
                 2.node1 172.16.210.12
                 3.node2 172.16.210.13
                 4.controller 172.16.210.10
####################################################################################
请输入你要连接的服务器编号: ^C
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: 
请输入你要连接的服务器编号: vip 
[root@db scripts]#  ##退出脚本,回到服务器

相关文章

  • linux使用shell脚本实现跳板机服务

    跳板机提前做好免密 只需要输入编号就可以成功连接各主机了 如果生产环境的跳板机有高安全性,可改良下脚本,使其跳板机...

  • Shell入门实例-应知应会

    需求在Linux服务器上运行jar包来启动Java服务,使用kill相关命令关闭服务,需要用shell脚本实现,s...

  • Linux总结

    Linux中编写Shell脚本 Ubuntu下使用tmux实现分屏,以及tmux快捷键

  • shell脚本实现连接远程服务器(ECS)

    原文地址:shell脚本实现连接远程服务器(ECS) 说明 本篇文章主要使用expect脚本来实现自动连接远程服务...

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • linux shell脚本攻略笔记

    LINUX SHELL脚本攻略笔记[速查] linux shell脚本攻略笔记

  • FTP自动上传文件

    在Linux中,有些时候为了简便,需要自动上传文件到FTP,本文教你如何在Linux下使用Shell脚本实现ftp...

  • 尚硅谷大数据技术之Oozie

    第4章 Oozie的使用4.1 案例一:Oozie调度shell脚本目标:使用Oozie调度Shell脚本分步实现...

  • 服务监控

    服务监控 利用swoole 通过linux来实现服务监控 通过模拟shell命令并使用swoole方法达到2秒执行...

  • Linux Shell:Shell脚本常用语法整理

    摘要:Linux,Shell脚本 Shell 是一个用 C语言编写的程序,它是用户使用 Linux 的桥梁,用户通...

网友评论

      本文标题:linux使用shell脚本实现跳板机服务

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