#!/bin/bash
#检测服务器端口是否开放,成功会返回0值显示ok,失败会返回1值显示fail
PWD=`pwd`
port=9888
date=`date +"%Y-%m-%d %H:%M:%S"`
Rsync_exec(){
expect -c "
set timeout 600;
spawn rsync -rpogtvae \"ssh -p 1918\" root@114..11.11.11:/home/python-ssh/hostlist /home/soft
expect {
\"*yes/no*\" {send \"yes\r\";exp_continue}
\"*password*\" {send \"xxxxx\r\";}
}
expect eof;"
}
Rsync_exec
echo "----------------------$date 以下游戏服9888端口没启动请检查!--------------------" >$PWD/port_closed_status
while read line
do
nc -w 40 -z $line $port > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$line--$port端口status: 开启!"
else
echo "$line--$port端口status: 关闭!"
echo "$line--$port端口status: Closed" >>$PWD/port_closed_status
fi
sleep 1;
done<$PWD/hostlist
cat $PWD/port_closed_status
#send mail
closed=`cat $PWD/port_closed_status | grep Closed`
if [ "$closed" != "" ]
then
iconv -f utf-8 -t gb2312 $PWD/port_closed_status | tee $PWD/port_closed_status.txt > /dev/null
mail -s "Server Port Status " xxxxxxx@qq.com < $PWD/port_closed_status.txt -- -f monitor@t4game.com
fi