简单的JumpServer
1、需求:工作中,我们需要管理N多个服务器。那么访问服务器就是一件繁琐的事情。通过shell编程,编写跳板程序。当我们需要访问服务器时,看一眼服务器列表名,按一下数字,就登录成功了。
2、分析:
- 运行跳板脚本。弹出数十台服务器名的菜单
- 1)web1
- 2)web2
- 3)web3
- ...........
- q)quit
- 用户请选择要连接的主机[1-3]:1
- Last login: Tue Nov 21 22:23:09 2023 from 192.168.151.1 #成功登陆
[root@localhost ~]# vim jump.sh #脚本编写
#!/bin/bash
#定义目标主机ip
web1=192.168.151.151
web2=192.158.151.152
web3=192.168.151.153#打印菜单
cat <<EOF
目标服务器列表:
1.web1
2.web2
3.web3
4.web4
5.mysql1
6.mysql2
7.docker1
q.quit
EOF#读取用户输入
read -p "请输入管理服务器序号:" num#判断用户选择
case $num in
1)
ssh root@$web1
;;
2)
ssh root@$web2
;;
3)
ssh root@$web3
;;
q)
echo "谢谢,再见"
;;
esac[root@localhost ~]# chmod +x jump.sh #脚本授权[root@localhost ~]# ./jump.sh #执行脚本
目标服务器列表:
1.web1
2.web2
3.web3
4.web4
5.mysql1
6.mysql2
7.docker1
q.quit
请输入管理服务器序号:1
The authenticity of host '192.168.151.151 (192.168.151.151)' can't be established.
ECDSA key fingerprint is SHA256:1v+zhimsbT04H97F0PMoe28USxNkJac201tQ233d9pE.
ECDSA key fingerprint is MD5:a1:d0:5a:af:0f:5d:0a:f7:68:b0:9d:13:ed:43:51:ff.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.151.151' (ECDSA) to the list of known hosts.
root@192.168.151.151's password:
Last login: Tue Nov 21 22:23:09 2023 from 192.168.151.1
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt/cdrom 忙/dev/sr0 已经挂载到 /mnt/cdrom 上
[root@localhost ~]#