shell跳板机(较复杂)

创建 jumpserver.sh

创建跳板机存放目录:
[root@jumpserver ~]# mkdir /home/jumpserver

[root@jumpserver ~]# cd /home/jumpserver/

创建脚本:
[root@jumpserver jumpserver]# vim jumpserver.sh
#!/bin/bash

#定义变量
ip=`/sbin/ifconfig ens33|sed -n "2p"|awk '{print $2}'`
IPX=(
ip
ip
ip

)
current_date=`date +%Y-%m-%d-%H:%M:%S`
dis_manumenu=""

#远程服务器的账号:
user="root"
# Source function library. . /etc/rc.d/init.d/functions

#定义规则
new_echo () {
if [ -z "$2" ];then
    COLOR=33
elif [ $2 == red ];then
    COLOR=31
elif [ $2 == green ];then
    COLOR=32
elif [ $2 == blue ];then
    COLOR=36
elif [ $2 == purple ];then
    COLOR=35
elif [ $2 == white ];then
    COLOR=39
else
       echo 'new_echo function use error'
       exit
fi

if [ -z "$4" ];then COLOR1=33
elif [ $4 == red ];then COLOR1=31
elif [ $4 == green ];then COLOR1=32
elif [ $4 == blue ];then COLOR1=36
elif [ $4 == purple ];then COLOR1=35
elif [ $4 == white ];then COLOR1=39
else
     echo 'new_echo function use error'
     exit
fi
if [ -z "$3" ];then
        echo -en "\033[1;${COLOR}m""\033[3m$1 \033[0m\n"
   else
        echo -en "\033[1;${COLOR}m""\033[3m$1 \033[0m\033[70G\033[1;${COLOR1}m\033[3m $3 \033[0m\n"
fi
}

#信号捕捉
signals () {
for signal in `seq 1 1000`
  do
    trap ':' INT EXIT TSTP TERM HUP $signal &> /dev/null
done
clear
for signal in `$name`
  do
        trap ':' INT EXIT TSTP TERM HUP $signal &> /dev/null
done
clear
}
signals
#回显函数(隐藏明文信息)
getchar () {
    stty cbreak -echo
    dd if=/dev/tty bs=1 count=1 2>/dev/null
    stty eof ^c -cbreak echo 
}

named () {
while :
  do
    ret=`getchar`
    if [ x$ret =  x ];then
        echo
        stty eof ^c
        break
    fi
    str="$str$ret"
    printf "*"
done

echo "$str" >~/nameserver.p
}

#判断账号登陆部分

public_user () {
public=~
if [ -z $CHECK ];then
       clear
       new_echo "----------------------------------------------------------------------------------" green
       new_echo "Connection '$ip'($USER)" purple "[ SUCCESS ]" green
       new_echo "`date +'%x %X'` " white
       new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green
       echo 用户:$user-"现在开始登陆,请稍等..............................."
       /bin/true
       ssh  $user@$ip
       sleep 2
fi
}

################################################################################

#验证账号登录
while :
   do
    printf '''

  请输入登陆跳板机验证标识:'''
    named
    name=`cat ~/nameserver.p`
        repa=`cat /opt/username.p`
    if [  "$name" == "$repa" ];then
        echo '''

    认证标识正确,欢迎登陆跳板机.............

'''
        /bin/true
        break
      else
        echo '''
   
    对不起,您输入的认证标识有错误,无法登陆跳板机,已结束操作............'''
        /bin/false
        exit
    fi
done
rm -f ~/nameserver.p
sleep 1

#生成密钥:
`which ssh-keygen` -t rsa -P '' <<EOF

n
EOF
clear

#登录界面
    echo -e "\033[31m---------------------请输入需要登录服务器的ip、账号、密码等信息----------------------\033[0m\n"
    read -p "服务器ip:" host_ip
    if [ -z $host_ip ];then
        read -p "请输入有效的服务器ip地址:" host_ip
        if [ -z $host_ip ];then
            /bin/true
            exit
        fi
    fi
    read -p "服务器用户(默认回车是root):" host_name
    if [ ! -z $host_name ];then
        read -p "ssh服务端口号(默认回车是22):" host_post
        if [ -z $host_post ];then
            ssh  $host_name@$host_ip
            /bin/true
            exit
           else
            ssh  $host_name@$host_ip -p $host_post
            /bin/true
            exit
        fi
      else
        read -p "ssh服务端口号(默认回车是22):" host_post
        if [ -z $host_post ];then
            ssh  root@$host_ip
            /bin/true
            exit
           else
            ssh  root@$host_ip -p $host_post
            /bin/true
            exit
        fi
    fi
    /bin/true
    exit
    clear
    echo 账户:$USER-"您的输入有误,已退出,请重新登录,谢谢................"
    /bin/true
    exit
更改脚本权限(可执行):    
[root@jumpserver jumpserver]# chmod 755 jumpserver.sh
[root@jumpserver jumpserver]# chown root.root /home/jumpserver/jumpserver.sh

说明: 至此跳板机以搭建完成,以下是一些菜单选项,可以根据个人需求选择使用!!!

添加菜单选项

使用本菜单选项的方法:将上面jumpserver.sh脚本中“登录界面”下的参数换成下面的参数即可。

cat <<EOF
    当前系统时间:$current_date 
    =============================================================================
         $dis_manumenu 当前使用跳板机用户:$USER 版本:Versin1.0
    =============================================================================    
    ------------------------------跳板机帮助手册---------------------------------
    (1)回车或输入"命令菜单"以外的任意字符,是退出跳板机服务。
    (2)命令菜单"0"自定义跳转(即:ssh 任意ip)
    (3)菜单1以上的选项是服务器列表,这里只有12台服务器提供选择。
    (4)跳板机上面服务器包含:各项目存储服务器,日志服务器,应用服务。
    -----------------------------------------------------------------------------
    以下为命令菜单,请选择:

    **0)(自定义输入服务器ip、账号和密码等)
        -->[---------------------------公司服务器菜单---------------------------]:<--
    **1)利华app项目 ${IPX[0]} (群集数:8)
    **2)别克app项目 ${IPX[1]} (群集数:18)
    **3)日志服务器项目 ${IPX[2]} (群集数:11)
    **4)测试服I 未使用 (群集数:13)
    **5)测试服II 未使用 (群集数:13)
    **6)测试服III 未使用 (群集数:13)
    **7)测试服IV 未使用 (群集数:13)
    **8)测试服V 未使用 (群集数:13)
    **9)存储服务器 未使用(群集数:13)
    **10)测试服VI 未使用 (群集数:13)
    **11)本地测试服I 未使用 (群集数:13)
    **12)本地测试服II 未使用 (群集数:13)
    ##############################################################################
    ==============================================================================
EOF

read -p "请选择(例子:1 ): " num

case $num in
1)
    ip=${IPX[0]}
    public_user
;;
2)
    ip=${IPX[1]}
    public_user
;;
3)
        ip=${IPX[2]}
    public_user

;;
0)
    echo -e "\033[31m---------------------请输入需要登录服务器的ip、账号、密码等信息----------------------\033[0m\n"
    read -p "服务器ip:" host_ip
    if [ -z $host_ip ];then
        read -p "请输入有效的服务器ip地址:" host_ip
        if [ -z $host_ip ];then
            /bin/true
            exit
        fi
    fi
    read -p "服务器用户(默认回车是root):" host_name
    if [ ! -z $host_name ];then
        read -p "ssh服务端口号(默认回车是22):" host_post
        if [ -z $host_post ];then
            ssh  $host_name@$host_ip
            /bin/true
            exit
           else
            ssh  $host_name@$host_ip -p $host_post
            /bin/true
            exit
        fi
      else
        read -p "ssh服务端口号(默认回车是22):" host_post
        if [ -z $host_post ];then
            ssh  root@$host_ip
            /bin/true
            exit
           else
            ssh  root@$host_ip -p $host_post
            /bin/true
            exit
        fi

    fi
    /bin/true
    exit
;;
*)
    clear
    echo 账户:$USER-"您的输入有误,已退出,请重新登录,谢谢................"
    /bin/true
    exit
;;
esac

编辑普通用户局部变量文件

[root@jumpserver jumpserver]# vim /home/jumpserver/.bashrc
sh ~/jumpserver.sh
exit

配置认证文件

[root@jumpserver jumpserver]# vim /opt/username.p
adai

说明: 该文件的作用是设定在使用jumpserver.sh时的跳板机验证标识。

为跳板机配置公钥

以下内容只在普通用户测试使用。。。

创建用户密码文件

在/home/junpserver目录下创建file_useradd.txt:

[root@jumpserver jumpserver]# vim file_useradd.txt
#文件内容格式:账号       密码
192.168.8.130  123456

之后直接执行脚本即可!

[root@jumpserver jumpserver]# vim /home/jumpserver/ssh-key-add.sh
#!/bin/bash

#定义账号密码:
#PWDD='XXXX'

#定义账号密码文件(必须修改)
num=`cat /home/jumpserver/file_useradd.txt | wc -l`
WCD=`wc /home/jumpserver/file_useradd.txt |awk '{print $2}'`

#定义主机
#ZHUJI=(
#ip1
#ip2
#)

#------------------------------------------------------------------------------------#

#生成密钥:
`which ssh-keygen` -t rsa -P '' <<EOF

y
EOF

#调用交互函数
/tmp/ssh-key.expect
#!/usr/bin/expect 
set timeout 10 
set username [lindex \$argv 0] 
set password [lindex \$argv 1] 
set hostname [lindex \$argv 2] 
spawn ssh-copy-id  \$username@\$hostname
expect {
            #first connect, no public key in ~/.ssh/known_hosts
            "Are you sure you want to continue connecting (yes/no)?" {
            send "yes\r"
            expect "password:"
            send "\$password\r"
            expect "]*"
            }
            #already has public key in ~/.ssh/known_hosts
            "password:" {
                send "\$password\r"
            }
            "Now try logging into the machine" {
                #it has authorized, do nothing!
            }
        }
expect eof


sleep 1
sudo chmod 777 /tmp/ssh-key.expect

#执行过程
if [ "$WCD" -ne 0 ];then
        for i in `seq 1 $num`
        do
        user_host=`awk 'NR=='$i'{print $1}' /home/jumpserver/file_useradd.txt`
        password_host=`awk 'NR=='$i'{print $2}' /home/jumpserver/file_useradd.txt`
         /tmp/ssh-key.expect root  $password_host $user_host
        wait
        done
fi

#删除临时文件
rm -f /tmp/ssh-key.expect

转载于:https://my.oschina.net/adailinux/blog/1544808

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值