前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell跳板机(较复杂) 转

shell跳板机(较复杂) 转

作者头像
阿dai学长
发布2019-04-03 10:57:26
8870
发布2019-04-03 10:57:26
举报
文章被收录于专栏:阿dai_linux

创建 jumpserver.sh

代码语言:javascript
复制
创建跳板机存放目录:
[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脚本中“登录界面”下的参数换成下面的参数即可。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
[root@jumpserver jumpserver]# vim /home/jumpserver/.bashrc
sh ~/jumpserver.sh
exit

配置认证文件

代码语言:javascript
复制
[root@jumpserver jumpserver]# vim /opt/username.p
adai

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

为跳板机配置公钥

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

创建用户密码文件

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

代码语言:javascript
复制
[root@jumpserver jumpserver]# vim file_useradd.txt
#文件内容格式:账号       密码
192.168.8.130  123456

之后直接执行脚本即可!

代码语言:javascript
复制
[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

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/09/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建 jumpserver.sh
    • 添加菜单选项
    • 编辑普通用户局部变量文件
    • 配置认证文件
    • 为跳板机配置公钥
      • 创建用户密码文件
      相关产品与服务
      日志服务
      日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档