想法:禁止用户通过ssh直接远程到应用主机上,通过限定用户登录,利用运维主机跳转到用户有权限登录的应用主机上,并对其操作进行记录,实现堡垒机的跳转功能。

shell脚本创建用户登录的选择菜单,expect脚本实现自动交互功能,设定用户登录到运维机后自动运行选择菜单,并禁止用户使用ctrl+c强制退出,安装gateone,通过web浏览器使用ssh

环境:centos 6  x86

以下是个脚本代码:

1、menu.sh

#!/bin/bash
#清屏
clear

#禁止在脚本中使用ctrl+c强制退出
#trap "" SIGINT

#给user赋值
user=`whoami`

#当选择完选项,且执行完命令后,返回选择菜单
while :
do

#菜单选择项
cat << EOF
********please enter your choise********
(1) The management switch
(2) The management host
(3) Exit Menu
EOF
read -p "Now select the top option to: " input
case $input in 
1) 
    echo "Enter a switch IP:"
     read file
      #逐行读取switch文件中的内容
       for line in = `cat /tmp/switchs.txt`
       do
      #设定判断条件,如果file模糊匹配line中的内容,则运行expect脚本
       if [[ $file =~ $line ]] 
           then
             server=`echo $line |awk -F "|" '{print $1}'`
             login=`echo $line |awk -F  "|" '{print $2}'`
             passwd=`echo $line |awk -F "|" '{print $3}'`
             enpasswd=`echo $line |awk -F "|" '{print $4}'`

           /tmp/telnet.exp $server $login $passwd $enpasswd
           
       fi
     done ;;
2) 
    echo "Enter a host IP:"
    read file
      for line in = `cat /tmp/hosts.txt`
       do
       if [[ $file =~ $line ]]
           then
             server=`echo $line |awk -F "|" '{print $1}'`
             passwd=`echo $line |awk -F "|" '{print $2}'`
  
           /tmp/ssh.exp $server $passwd
       fi
     done ;;

3)
  #关闭当前用户连接
  pkill -u$user
  ;;
   
esac
done

2、ssh.exp

#!/usr/bin/expect -f 

if { $argc != 2 } {  
send_user "usage: $argv0 <server> <password>\n"  
exit  
}  
   
set timeout 1 
set TERM ANSI  
  
set SERVER [lindex $argv 0]  
set PASSWD [lindex $argv 1]
set USER [exec sh -c {whoami}]       #exec sh -c {}此语句是在expect脚本里执行shell语句

spawn -noecho /usr/bin/ssh root@$SERVER            #-noecho参数是运行时不显示命令ssh命令
sleep 5

#用户第一次用ssh登录应用主机,会询问是否永久保存 RSA 到应用主机的 know hosts 列表中,所以这里expect抓取的时候判断下
expect {                                           
       "(yes/no)"
        {
          send "yes\r"
           expect {
               "Password:"  
                {
                 send "$PASSWD\r"
                }
              }
         }
        "Password:"
          {
           send "$PASSWD\r"
          }
       }
expect eof
sleep 1 

interact

3、telnet.exp 

#!/usr/bin/expect -f 

if { $argc != 4 } {  
send_user "usage: $argv0 <server> <login> <password> <enpassword>\n"  
exit  
}  
   
set timeout 1 
set TERM ANSI  
  
set SERVER [lindex $argv 0]  
set LOGIN  [lindex $argv 1]
set PASSWD [lindex $argv 2]  
set ENPASSWD [lindex $argv 3]  
  
spawn /usr/kerberos/bin/telnet
expect "telnet> "  
send "open $SERVER\r"
sleep 10
expect "login:"
send "$LOGIN\r"
sleep 1
expect "Password:"  
send "$PASSWD\r"  
sleep 1
expect "*>"  
send "en\r"  
sleep 1  
expect "Password:"  
send "$ENPASSWD\r"  
sleep 1  

interact

4、hosts.txt

172.*.*.*|password
172.*.*.*|password

5、switchs.txt

10.*.*.*|login|password|enpassword
10.*.*.*|login|password|enpassword

6、设置用户登录自动弹出选择菜单,此处以test用户为例,在test用户目录下编辑.bashrc文件,在最后一行添加需要自动运行的脚本

cat /home/test/.bashrc 
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

/home/test/menu.sh

以上是实现了选择菜单功能、自动交互功能和自动运行脚本功能。效果如下:

wKiom1TLTtygTJ2HAAPXxvaMNd8389.jpg


7、安装gateone (补充下:gateone不支持IE浏览器,chrome或firefox都可以)

  • 所需软件:gcc、python2.7.2、gateone1.1.1(支持python2.6以上版本)、tornado-2.4-1、python-ordereddict-1.1-2、epel-release-6-8.noarch.rpm。

  • 下载地址(也可到http://down.51cto.com/data/1980081下载我打好的压缩包):

https://github.com/downloads/liftoff/GateOne/tornado-2.4-1.noarch.rpm
https://github.com/downloads/liftoff/GateOne/gateone-1.1-1.noarch.rpm
ftp://ftp.univie.ac.at/systems/linux/fedora/epel/6/x86_64/python-ordereddict-1.1-2.el6.noarch.rpm
rpm -ivh http://mirrors.sohu.com/fedora-epel/6Server/x86_64/epel-release-6-8.noarch.rpm
  • Python安装

cd Python 2.7.2
./configure --prefix=/usr/local/Python
make
make install
  • gateone安装

rpm -ivh tornado-2.4-1.noarch.rpm
rpm -ivh gateone-1.1-1.noarch.rpm
rpm -ivh python-ordereddict-1.1-2.el6.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
  • gateone默认安装到/opt/gateone,且自动在/etc/init.d下添加了服务,很人性化。

  • 启动gateone:service gateone start,第一次启动后,会在/opt/gateone目录下生成server.conf文件。

  • 修改server.conf文件,

user_dir = "/var/log/gateone/users" 
#存放用户登录的session记录,也就是录屏文件。
              
origins = "http://localhost;https://localhost;http://127.0.0.1;https://127.0.0.1;https://localhost;https://localhost.localdomain;https://localhost4;https://localhost4.localdomain4;https://localhost.localdomain;https://localhost6;https://localhost6.localdomain6;https://127.0.0.1;https://127.0.0.1;https://10.207.252.2"
#gateone默认只可以本地登录,所以要添加登录地址,本机地址为10.207.252.2,在后面添加上即可

log_file_prefix = "/var/log/gateone/webserver.log"
#系统日志存储地址
  • 修改完server.conf文件后,重启gateone。

在浏览器里输入https://ip,进去后跟使用终端软件访问ssh的方式一模一样。

wKioL1TLS6mCtmn_AAEsaq1l9Eg495.jpg

  • 会话回放

  wKiom1TLTWyD8e_JAAHRqO04E-k983.jpg

  •  实时会话回放,gateone支持实时回放,点击下面的播放箭头,即可将用户之前操作的命令,回放在屏幕上。

wKioL1TLTnTjypK9AAJ1yxShP-c804.jpg