歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux下trap+shell三層目錄專業規范跳板機腳本

Linux下trap+shell三層目錄專業規范跳板機腳本

日期:2017/3/3 11:45:48   编辑:SHELL編程
跳板機安全防范規劃:
跳板機-> 中心機 -> 後端服務器
跳板機:用來做跳板登陸中心機用的
中心機:一般用來做分發用的,用來登陸後端服務器
在內網:一般直接連中心機,然後直接連接後端服務器
在外網:為了安全,一般會先通過普通用戶連接跳板機,然後再連接中心機,然後再在中心機連接後端服務器
注意:普通用戶是不讓登陸到跳板機終端的
三層目錄跳板腳本實現功能:
1、通過普通用戶連接跳板再直接連接後端服務器
2、ssh+key認證,CRT代理轉發功能開啟,自別識別用戶,無需用戶再輸入用戶名、密碼和端口
3、trap信號捕捉不允許用戶Crtl+C退出腳本,跳板腳本普通用戶一登陸就會自動運行
注意:這裡只展示qqandroid平台的其中一台服務器的ssh連接,其他平台服務器的ip連接原理一樣
#創建腳本目錄
mkdir -p /scripts/tiaoban/{qqandroid,qqios,qqwxandroid,wxios}

#禁止普通用戶登陸
cat /etc/profile.d/tiaoban.sh
[ $UID -ne 0 ] && . /scripts/tiaoban/tiaoban.sh

#腳本目錄結構
[root@Master /]# tree /scripts/tiaoban/
/scripts/tiaoban/
├── qqandroid
│   ├── 1_100.sh
│   ├── ip.txt
│   └── qqandroid.sh
├── qqios
├── qqwxandroid
├── tiaoban.sh
└── wxios

4 directories, 4 files

#跳板腳本第一層目錄,
cat /scripts/tiaoban/tiaoban.sh                             
#!/bin/bash
function trapper() {
trap ':' 1 2 3 20 15
}
while :
do 
trapper
clear
cat <<menu                       
    1)qqandroid 
    2)qqios(功能未開發)
    3)wxandroid(功能未開發)
    4)wxios(功能未開發)
    5)exit
menu
read -p 'please select a num:' num
case "$num" in
1)
 sh /scripts/tiaoban/qqandroid/qqandroid.sh
 ;;
2)
 sh /scripts/tiaoban/qqios/qqios.sh
 ;;
3)
 sh /scripts/tiaoban/wxandroid/wxandroid.sh
 ;;
4)
 sh /scripts/tiaoban/wxios/wxios.sh
 ;;
5)
 exit
 esac
done

#跳板機腳本第二層目錄
cat /scripts/tiaoban/qqandroid/qqandroid.sh
#!/bin/bash
function trapper() {
        trap ':' 1 2 3 20 15
}
while :
do
        trapper
        clear
        cat <<menu                     
        1)qq1-100服
        2)qq101-200服(功能未開發)
        3)qq201-300服(功能未開發)
        4)qq301-400服(功能未開發)
        5)返回上一級
        6)exit
menu
read -p 'please select a num:' num
case "$num" in
1)
 sh /scripts/tiaoban/qqandroid/1_100.sh
 ;;
2)
 sh /scripts/tiaoban/qqandroid/101_200.sh
 ;;
3)
 sh /scripts/tiaoban/qqandroid/201_300.sh
 ;;
4)
 sh /scripts/tiaoban/qqandroid/301_400.sh
 ;;
5)
 sh /scripts/tiaoban/tiaoban.sh
 ;;
6)
 exit
 esac
done

#跳板機腳本第三層目錄
cat /scripts/tiaoban/qqandroid/1_100.sh
#!/bin/bash
function trapper() {
        trap ':' 1 2 3 20 15
}
while :
do                                    
        trapper
        clear
        cat <<menu               
        1)qq1服
        2)qq2服(功能未開發)
        3)qq3服(功能未開發)
        4)qq...服(功能未開發)
        5)返回上一級目錄
        6)返回首頁
        7)退出
menu
read -p 'please select a num:' num
yh=`id`
user=`echo $yh | awk -F" " '{print $2}' | awk -F"gid=" '{print $2}' | awk -F"(" '{print $2}' | awk -F")" '{print $1}'`
case "$num" in
1)
 ip=`head -$num /scripts/tiaoban/qqandroid/ip.txt`
 ssh -p 22 $user@$ip
 ;;
2)
 ip=`head -$num /scripts/tiaoban/qqandroid/ip.txt`
 ssh -p 22 $user@$ip
 ;;
3)
 ip=`head -$num /scripts/tiaoban/qqandroid/ip.txt`
 ssh -p 22 $user@$ip
 ;;
4)
 ip=`head -$num /scripts/tiaoban/qqandroid/ip.txt`
 ssh -p 22 $user@$ip
 ;; 
5)
  sh /scripts/tiaoban/qqandroid/qqandroid.sh
 ;; 
6)
  sh /scripts/tiaoban/tiaoban.sh
 ;;
7)
 exit
 esac
done

#ip.txt文件展示
[root@Master ~]# cat /scripts/tiaoban/qqandroid/ip.txt 
192.168.17.215
192.168.17.216
192.168.17.217
192.168.17.218
192.168.17.219
192.168.17.220
192.168.17.221
192.168.17.222
192.168.17.223
192.168.17.224
192.168.17.225

#腳本執行展示
1)普通用戶登陸



2)第一層目錄



3)第二層目錄



4)第三層目錄和登陸成功展示



注意:在三層任意一層普通用戶都按crtl+c都是無效的,且在第三層選擇exit退出會回到第二層,在第二層選擇exit會回到第一層,在第一層選擇exit會注銷終端重新執行跳板腳本,普通用戶是沒法登陸到跳板機終端的,周知
本文出自 “wsyht的博客” 博客,請務必保留此出處http://wsyht2015.blog.51cto.com/9014030/1791938
Copyright © Linux教程網 All Rights Reserved