检查服务器端口是否存活,端口不存活微信告警

脚本添加到计划任务定时执行即可,已在原脚本上做了优化,多端口检查,只需要修改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:
$Content"}, "safe":"0"}" $PURL &>/tmp/weixin.log

}

if [[ $number -ge 1 ]];then
        weixin
fi

rm -f $TMP1
Port_Check

 Centos 7 无法使用" nc -z "命令,以下是正对于Centos 7 优化过脚本

#!/bin/bash

##iplist fomart (QCloud)
##resourseid    主机名    操作系统    公网IP    内网IP
##ins-24***        Game_服    centos7        122.152.2.2**    10.105.15.139

TMP1=`mktemp`
FILE=iplist
PORT=(80,555)
Count=${#PORT[@]} 
CropID='wxfbd185be0396d***'
Secret='7YDLwYWE9SGHUxgGsrFqFA4zT4ZYuD0PEeCWZGbmMVGDjQbuIo1snjQpn0dn1vuS'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL |  awk -F "[":,]" '{print $15}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

function body() {
    local int AppID=2
    local UserID="huangrui"
    Ent=$'
'
    Date=$(date '+%Y年%m月%d日 %H:%M:%S

')
    Tit="服务器端口扫描"
    Content=`cat $TMP1`
    Msg=$Date$Ent$Tit$Ent$Content
    printf '{
'
    printf '	"touser": "'"$UserID""",
"
    printf '	"msgtype": "news",
'
    printf '	"agentid": "'" $AppID """,
"
    printf '	"news": {
'
    printf '	"articles": [
'
    printf '{
'
    printf '		"title": "'"$Tit"","
"
    printf '		"description": "'"$Msg"","
"
    printf '	}
'
    printf '	]
'
    printf '	}
'
    printf '}
'
}

scan_port(){
    IP=`grep -vE "^#|^$|Xserver" $FILE|sed -n "${j}p" | awk '{print $4}'` 
    if [[ -n $IP ]];then
        SITE=`grep -w $IP $FILE|awk '{print $2}'`
        for (( p=0;p<${Count};p++ ))
        do
        echo ""|nc -w 10 $IP ${PORT[p]} > /dev/null 2>&1
        if [ $? != 0 ];then
            echo ""|nc -w 10 $IP ${PORT[p]} > /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=10
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`

if [[ $number -ge 1 ]];then
    curl -l -H "Content-type: application/json" -X POST -d "$(body )" $PURL
fi

rm -f $TMP1
Centos7_Port_Check.sh

 shell 遍历数组2种方式

#!/bin/bash

LIST=(10001 10002 10003)
COUNT=${#LIST[@]} 
for (( p=0;p<${COUNT};p++ ))
do
    echo ${LIST[p]}
done

LIST1="20001 20002 20003"
for i in `echo $LIST1`
do
    echo $i
done
read list
原文地址:https://www.cnblogs.com/Mrhuangrui/p/6590203.html