课前回顾
图形化跳板机
#!/bin/bash
lb01=10.0.0.5
lb02=10.0.0.6
web01=10.0.0.7
web02=10.0.0.8
web03=10.0.0.9
nfs=10.0.0.31
backup=10.0.0.41
db01=10.0.0.51
db02=10.0.0.52
m01=10.0.0.61
zabbix=10.0.0.71
title="欢迎进入曾老湿跳板机-$m01"
#跳板机函数
JUMP(){
OPTION=$(whiptail --title "$title" --menu "请选择你要连接的服务器:" 25 60 11 \
"1" "连接 lb01" \
"2" "连接 lb02" \
"3" "连接 web01" \
"4" "连接 web02" \
"5" "连接 web03" \
"6" "连接 nfs" \
"7" "连接 backup" \
"8" "连接 db01" \
"9" "连接 db02" \
"10" "连接 m01" \
"11" "连接 zabbix" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
case $OPTION in
1)
connect $lb01
;;
2)
connect $lb02
;;
3)
connect $web01
;;
4)
connect $web02
;;
5)
connect $web03
;;
6)
connect $nfs
;;
7)
connect $backup
;;
8)
connect $db01
;;
9)
connect $db02
;;
10)
connect $m01
;;
11)
connect $zabbix
;;
12)
connect $te
;;
13)
connect $te_web
;;
esac
fi
}
#主机函数
HOST_INFO (){
HOST=$(whiptail --title "$title" --checklist \
"请选择要发送的主机名:" 25 60 11 \
"$lb01" "发送给lb01" OFF \
"$lb02" "发送给lb01" OFF \
"$web01" "发送给web01" OFF \
"$web02" "发送给web02" OFF \
"$web03" "发送给web03" OFF \
"$nfs" "发送给nfs" OFF \
"$backup" "发送给backup" OFF \
"$db01" "发送给db01" OFF \
"$db02" "发送给db02" OFF \
"$m01" "发送给m01" OFF \
"$zabbix" "发送给zabbix" OFF 3>&1 1>&2 2>&3)
}
SER_INFO(){
SER=$(whiptail --title "$title" --checklist \
"请选择要检查的服务:" 25 60 10 \
"nginx" "检查nginx" OFF \
"mysqld" "检查mysqld" OFF \
"php" "检查php" OFF \
"tomcat" "检查tomcat" OFF \
"sshd" "检查sshd" OFF \
"httpd" "检查httpd" OFF \
"vsftpd" "检查vsftpd" OFF \
"docker" "检查docker" OFF \
"saltstack" "检查saltstack" OFF \
"rsyncd" "检查rsyncd" OFF 3>&1 1>&2 2>&3)
}
#连接函数
connect(){
whiptail --title "$title" --yesno "你确定要连接这台机器么?想好了啊!!!" 10 60
if [ $? -eq 0 ];then
{
ping -c 1 -w 1 $1 >/tmp/ping.txt 2>/dev/null
if [ $? -ne 0 ];then
for ((i = 0 ; i <= 100 ; i+=30)); do
sleep 1
echo $i
done
fi
} | whiptail --gauge "emmmm...我正在连接$1,检测网络中,等我一哈子..." 6 60 0
grep 'ttl' /tmp/ping.txt &>/dev/null
if [ $? -eq 0 ];then
ssh root@$1
else
whiptail --title "$title" --msgbox "网络检测失败,别连了,我的哥,$1机器都没开" 10 60
fi
fi
}
#推送文件函数
SCP (){
HOST_INFO
PA=$(whiptail --title "$title" --inputbox "请输入需要推送的文件本地路径:" 10 60 /etc/passwd 3>&1 1>&2 2>&3)
DEST=$(whiptail --title "$title" --inputbox "请输入需要分发到主机的哪个目录:" 10 60 /tmp 3>&1 1>&2 2>&3)
{
for H in ${HOST};do
echo "scp $PA ${H}:${DEST}"|bash &>/dev/null
for ((i = 0 ; i <= 100 ; i+=50));do
sleep 1
echo $i
done
done
} | whiptail --gauge "别着急,正在传送中..." 6 60 0
}
#检查磁盘函数
CHECK_DISK (){
HOST_INFO
for H in ${HOST};do
echo "ssh $H 'df -h' "|bash > /tmp/disk
whiptail --title "$(date +%F-%T) | $H 磁盘信息" --msgbox "$(cat /tmp/disk)" 10 60
done
}
#检查内存函数
CHECK_MEM (){
HOST_INFO
for H in ${HOST};do
echo "ssh $H 'free -m' "|bash > /tmp/meminfo
whiptail --title "$(date +%F-%T) | $H 内存信息" --msgbox "$(cat /tmp/meminfo)" 30 80
done
}
#查看服务状态
CHECK_SER (){
HOST_INFO
SER_INFO
for H in ${HOST};do
for S in ${SER};do
HO=`echo "$H"|awk -F \" '{print $2}'`
PROC=`ssh $HO "ps -ef|grep $S|wc -l"`
NUM=$( expr $PROC - 3 )
if [[ $PROC > 3 ]];then
whiptail --title "$(date +%F-%T) | $H 服务信息" --msgbox "${S} 存活 | 进程数:$NUM" 10 60
else
whiptail --title "$(date +%F-%T) | $H 服务信息" --msgbox "${S} 没有存活| 进程数:$NUM" 10 60
fi
done
done
}
#批量之心命令函数
EXEC_CMD(){
HOST_INFO
CMD=$(whiptail --title "$title" --inputbox "请输入要执行的命令:" 10 60 ifconfig 3>&1 1>&2 2>&3)
for H in ${HOST};do
HO=`echo "$H"|awk -F \" '{print $2}'`
RES=`ssh $HO "$CMD"`
whiptail --title "$(date +%F-%T) | $H 命令执行结果" --msgbox "$RES" 40 80
done
}
#退出函数
EXIT(){
pass=$(whiptail --title "$title" --passwordbox "请输入你的退出密码" 10 60 3>&1 1>&2 2>&3)
if [ $pass == '123456' ];then
exit
else
whiptail --title "$title" --msgbox "密码错误,你不是运维,小样的~~~" 10 60
continue
fi
}
whiptail --title "$title" --msgbox "曾老湿跳板机,给你全新不一样的feel,进去了,就不想出来" 10 60
#抓取键盘信号
trap "" HUP INT TSTP
while true;do
OPTION=$(whiptail --title "$title" --menu "请选择你的动作(轻点...)" 15 60 8 \
"1" "SSH远程连接" \
"2" "推送文件" \
"3" "查看磁盘空间" \
"4" "查看内存空间" \
"5" "查看服务状态" \
"6" "批量执行命令" \
"7" "轻松一下(game)" \
"8" "退出" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
case $OPTION in
1)
JUMP
;;
2)
SCP
;;
3)
CHECK_DISK
;;
4)
CHECK_MEM
;;
5)
CHECK_SER
;;
6)
EXEC_CMD
;;
7)
sh /root/eluosi.sh
;;
8)
EXIT
;;
esac
fi
done