脚本添加到计划任务定时执行即可,已在原脚本上做了优化,多端口检查,只需要修改Port数组即可
需要iplist文件
如:TestServer_0001 10.10.0.50 Web_TestServer_0001
以下是脚本文件:
#!/bin/bash #===================通过微信报警=================== TMP1=`mktemp` FILE=iplist PORT=(80 3389) Count=${#PORT[@]} scan_port(){ IP=`grep -v -E "^#|^$" $FILE|sed -n "${j}p" | awk '{print $2}'` if [[ -n $IP ]];then SITE=`grep -w $IP $FILE|awk '{print $1}'` for (( p=0;p<${Count};p++ )) do nc -nvz -w 10 $IP ${PORT[p]}|grep succeeded > /dev/null 2>&1 if [ $? != 0 ];then nc -nvz -w 10 $IP ${PORT[p]}|grep succeeded > /dev/null 2>&1 if [ $? != 0 ];then echo "-----------------------------------">>$TMP1 echo "$SITE $IP Port ${PORT[p]} Failed" >> $TMP1 fi fi done fi } num=`cat $FILE| wc -l` thread_num=30 tmp_fifofile="/tmp/$$.fifo" mkfifo $tmp_fifofile exec 6<>$tmp_fifofile rm $tmp_fifofile for (( i=0;i<$thread_num;i++ )) do echo done >&6 for (( j=1;j<=${num};j++ )) do read -u6 { scan_port >/dev/null 2>&1 echo >&6 }& done wait exec 6>&- num=`wc -c $TMP1|awk '{print $1}'` number=`cat $TMP1|grep -v -E "^$|-"|wc -l` weixin(){ #CropID 企业Id #Secret 管理组的凭证密钥 CropID="wx80179d3a3e****" Secret="ZyqFs4qfUiXcz8plHFbhCWkF3JEjj7vASkZjs8YTRqKxq1yAx-U46foyNXNKz2qw" GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #AccessToken是企业号的全局唯一票据,调用接口时需携带AccessToken Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $4}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" Content=`cat $TMP1` curl -l -H "Content-type: application/json" -X POST -d "{\"touser\":\"@all\",\"msgtype\":\"text\",\"toparty\":\"1\",\"agentid\":\" 1\",\"text\":{\"content\": \"Server Port Check Monitor:\n$Content\"}, \"safe\":\"0\"}" $PURL &>/tmp/weixin.log } if [[ $number -ge 1 ]];then weixin fi rm -f $TMP1