加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 17:07:32 所属栏目:安全 来源:网络整理
导读:一、首先做好SSH密钥验证(跳板机地址172.16.1.61) 1、以下操作命令在所有机器上操作: [test@m01~]$useraddtest#==要在所有机器上操作[test@m01~]$echo123456|passwd--stdintest#==要在所有机器上操作Changingpasswordforusertest.passwd:allauthenticatio

一、首先做好SSH密钥验证(跳板机地址172.16.1.61)

1、以下操作命令在所有机器上操作:

[test@m01~]$useraddtest#<==要在所有机器上操作
[test@m01~]$echo123456|passwd--stdintest#<==要在所有机器上操作
Changingpasswordforusertest.
passwd:allauthenticationtokensupdatedsuccessfully.

2、以下操作命令仅在跳板机上操作:

[root@m01scripts]#su-test
[test@m01~]$ssh-keygen-tdsa-P''-f~/.ssh/id_dsa>/dev/null2>&1
[test@m01~]$sshpass-p123456ssh-copy-id-i~/.ssh/id_dsa.pub"-oStrictHostKeychecking=no172.16.1.80"
Warning:Permanentlyadded'172.16.1.80'(RSA)tothelistofknownhosts.
Nowtryloggingintothemachine,with"ssh'-oStrictHostKeychecking=no172.16.1.80'",andcheckin:

.ssh/authorized_keys

tomakesurewehaven'taddedextrakeysthatyouweren'texpecting.

[test@m01~]$sshpass-p123456ssh-copy-id-i~/.ssh/id_dsa.pub"-oStrictHostKeychecking=no172.16.1.81"
Warning:Permanentlyadded'172.16.1.81'(RSA)tothelistofknownhosts.
Nowtryloggingintothemachine,with"ssh'-oStrictHostKeychecking=no172.16.1.81'",andcheckin:

.ssh/authorized_keys

tomakesurewehaven'taddedextrakeysthatyouweren'texpecting.

3、用户登录跳板机后即调用脚本(不能命令行管理跳板机),并只能按管理员的要求选单

3.1、脚本放在跳板机上:

[root@m01scripts]#echo'[$UID-ne0]&&./server/scripts/jump.sh'>/etc/profile.d/jump.sh
[root@m01scripts]#cat/etc/profile.d/jump.sh
[$UID-ne0]&&./server/scripts/jump.sh
[root@m01scripts]#catjump.sh
#!/bin/bash
trapper(){
trap':'INTEXITTSTPTERMHUP定义需要屏蔽掉信号
}
main(){
while:
do
trapper
clear
cat<<menu打印菜单
1)lb01-172.16.1.80
2)lb02-172.16.1.81
menu
read-p"Plsinputanum.:"num
case"$num"in
1)
echo'loginin172.16.1.80.'
ssh172.16.1.80
;;
2)
echo'loginin172.16.1.81.'
ssh172.16.1.81
;;
110)
read-p"yourbirthday:"char
if["$char"="0926"];then
exit
sleep3
fi
;;
*)
echo"selecterror."
esac
done
}
main

4、执行效果如下:

[root@m01scripts]#su-test
1)lb01-172.16.1.80
2)lb02-172.16.1.81
Plsinputanum.:1输入1进入172.16.1.80这台服务器
loginin172.16.1.80.
[test@lb01~]$cat/etc/hosts查看lb01的hosts文件
127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4
::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6
#10.0.0.82www.tiandi.com
#10.0.0.82bbs.tiandi.com
#10.0.0.83www.tiandi.com
#10.0.0.83bbs.tiandi.com
10.0.0.80www.tiandi.com
10.0.0.80bbs.tiandi.com
[test@lb01~]$logout
Connectionto172.16.1.80closed.
1)lb01-172.16.1.80
2)lb02-172.16.1.81
Plsinputanum.:2输入2进入172.16.1.81这台服务器
loginin172.16.1.81.
[test@lb02~]$按ctrl+d返回到菜单
[test@lb02~]$logout按ctrl+d返回到菜单
Connectionto172.16.1.81closed.
1)lb01-172.16.1.80
2)lb02-172.16.1.81
Plsinputanum.:110输入110进入跳板机命令提示符
yourbirthday:0926需要输入特别码才能进入,这是管理员通道,要保管好这个特别码
[root@m01scripts]#跳板机管理命令

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读