首页 > 微信 > 详细

UDP端口检查告警SHELL脚本(企业微信版机器人版)

时间:2020-01-17 17:07:45      阅读:145      评论:0      收藏:0      [点我收藏+]

脚本准备

  • 0Batch_Check.sh
  • 1port_check.sh
  • 2wechat_bot_alert.sh
  • CheckList

CheckList

#支持大/小写
10.1.1.5 Udp 53
127.0.0.1 Tcp 1234

0Batch_Check.sh

#!/bin/sh
while read LineX
do
    #echo $LineX
    ./1port_check.sh $LineX
done < ./CheckList

1 1port_check.sh

#!/bin/sh
#TCP UDP port check and alert
#useage: port_check.sh  IP  PROTOCOL  PORT

input_IP=$1
input_PROTOCOL=$2
input_PORT=$3

#将大小写转成临时小写
tmp_PROTOCOL=`echo ${input_PROTOCOL} | tr '[A-Z]' '[a-z]'`
#echo $tmp_PROTOCOL

if [[ "${tmp_PROTOCOL}" == "tcp" ]]; then
    abbreviate_PROTOCOL="S"
elif [[ "${tmp_PROTOCOL}" == "udp" ]]; then
    abbreviate_PROTOCOL="U"
else
    echo "Unknown PROTOCOL" && exit 1
fi

/usr/bin/nmap ${input_IP} -s ${abbreviate_PROTOCOL} -p ${input_PORT} |grep "$input_PORT/$tmp_PROTOCOL open" &>/dev/null
if [[ $? == 0 ]]
then
    echo "${input_IP} ${tmp_PROTOCOL} Port ${input_PORT} Check OK"
else
    #A调用自建告警应用
    ## ./2wechat_app_alert.py 5 "WarningIP: ${input_IP}" "DES: ${tmp_PROTOCOL} Port ${input_PORT} is DOWN!"
    #B调用群机器人
    ./2wechat_bot_alert.sh  "Warning!!!  ${input_IP} : ${tmp_PROTOCOL} Port ${input_PORT} is DOWN!"
fi

2 2wechat_bot_alert.sh

#!/bin/sh
#useage: bot_alert.sh a b c ...

/usr/bin/curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxx' -H 'Content-Type: application/json'    -d '
   {
        "msgtype": "text",
        "text": {
            "content": "'$*'",
            "mentioned_list":["ZhangSan","LaoLiu"]
        }
   }'

参考资料

学了一招:在json格式中引用shell变量的技巧,实际上是把 -d 后面的data分为了三个部分 ‘{...‘ + $* + ‘...}‘ 。
感谢博主:https://jaminzhang.github.io/shell/the-problem-of-curl-commit-json-data-include-shell-variables/

企业微信群机器人手册:https://work.weixin.qq.com/api/doc/90000/90136/91770

UDP端口检查告警SHELL脚本(企业微信版机器人版)

原文:https://www.cnblogs.com/max27149/p/12206600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!