shell搭建跳板机

一、环境介绍

跳板机-----192.168.2.30
目标-------192.168.2.31

 

二、配置免密钥登录(30、31)

//分别在30、31创建tb用户
# useradd tb

//分别在30、31为tb用户设置密码
# echo 123456|passwd --stdin tb

//切换到tb用户下(30)
# su - tb

//交互式创建密钥。如果有提示直接回车即可(30)
# ssh-keygen -t dsa

//查看是否成功(30)
# ll /home/tb/.ssh/

//将密钥copy到31(30)
# ssh-copy-id -i .ssh/id_dsa.pub "-p 22 tb@192.168.2.31"

 

三、在30上编写脚本

//创建tb01.sh
# touch tb01.sh
#!/bin/sh
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
Tarp01(){
   trap '' INT QUIT TSTP TERM HUP
}
Menu01(){
cat  <<EOF
==========Host  List==========
       1)192.168.2.31
       2)192.168.1.1
==============================
EOF
}
Server02(){
case $a in
  1)
    ssh 192.168.2.31
   ;;
  2)
    ssh 192.168.1.1
   ;;
  *)
   ;;
esac
}

Server01(){
Menu01
RedColor='\E[;31m'
Res='\E[0m'
   read -p "Select the server you want to connect to:" a
      if [ $a -ne 1 -a $a -ne 2 ]&>/dev/null;then
         clear
         echo -e "${RedColor}Select the specified server [1|2]${Res}"
      else
         Server02
      fi
}

main(){
Tarp01
   while true
    do
      Server01
   done
}
main
View Code
//将脚本属主和属组改为tb用户
# chown tb.tb tb01.sh

//将脚本放到tb用户目录下
# mv tb01.sh  /home/tb/

//并给执行权限
# cd /home/tb/
# chmod +x tb01.sh

 

四、在30上系统设置

 

//在/etc/profile.d/目录下添加以下脚本
# cd /etc/profile.d/tbuser.sh
#!/bin/sh
if [ $UID -gt 500 ];then
   sh /home/tb/tb01.sh
fi
View Code

 

五、完成

然后用连接工具重新连接30,连接时使用tb用户连接即可

 

posted @ 2017-05-11 14:43  51redhet  阅读(1228)  评论(0编辑  收藏  举报