拨测ip+port 告警 telnet nc
1.telnet
[root@master-node156 mon_port]# cat mycheck_port|egrep -v '^$' #! /bin/sh #企业微信警配置 alert=True #配置True为启动告警,其它为不开启 #企业微信机器人ID userkey="5642127c-woab-shib-9da8-ac2f4adashabic003" echo 'a' >/tmp/problemip.txt cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` function check_port { pkill -9 telnet timeout 2 ssh 127.0.0.1 telnet $1 >/tmp/a.txt myresult=`egrep -i 'Escape' /tmp/a.txt|wc -l` if [[ $myresult -eq 1 ]];then echo "$cur_datetime 端口$port listening,status is ok..." | tee -a $homeDir/port_status.log else echo "$cur_datetime Error 端口$port closed,status is failed..." | tee -a $homeDir/port_status.log if [ "$alert" = "True" ];then echo $1 >/tmp/problemip.txt sed -i 's# #-#' /tmp/problemip.txt hostport=`cat /tmp/problemip.txt` curl -s -X POST -H "'Content-type':'application/json'" -d '{ "msgtype": "text", "text": { "content": "端口未启动 \r 主机端口: '$hostport' \r 时间: '$cur_datetime'", "mentioned_list":["wangqing",""], "mentioned_mobile_list":["",""] } }' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=$userkey fi sleep 1 fi } for i in `cat /opt/mon_port/8080.txt`;do a=`echo $i|awk -F[:] '{print $1" "$2}'`;check_port "$a"; done
2.nc
[root@pmmprd1-awssec2-web01 pmm-user]# cat mycheck_port.sh #! /bin/sh cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` function check_port { nc -w 3 -z $1 $2 > /dev/null 2>&1 echo $? echo 'after nc' if [ $? = 0 ] ; then echo "$cur_datetime 端口 $1 $2 listening,status is ok..." | tee -a $homeDir/port_status.log sleep 2 else echo "$cur_datetime Error 端口$1 $2 closed,status is failed..." | tee -a $homeDir/port_status.log sleep 2 fi } while read line do echo $line check_port $line done < /home/pmm-user/ip_port.txt echo "================================"
[root@pmmprd1-awssec2-web01 pmm-user]# cat /home/pmm-user/ip_port.txt
10.117.16.102 8030
10.117.16.102 38030
[root@master-node156 mon_port]# cat check_port |grep -v '^$' #! /bin/sh #企业微信警配置 alert=True #配置True为启动告警,其它为不开启 #企业微信机器人ID userkey="5wo2127c-shib-458b-9da8-ac2fshabidc003" while getopts ":h:p:" option do case "$option" in h) host=$OPTARG;; p) port=$OPTARG;; *) echo "Usage: args [-h] [-p]" echo "-h means host" echo "-p means port" exit 1 ;; esac done if [ ! $port ];then echo "请输入端口号,如 $0 -h 192.168.1.10 -p 80" echo "没有-h参数,默认检查本地端口" exit 1 fi cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` localIp=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -n 1) if [ ! $host ];then host=$localIp fi #res=`$homeDir/check_tcp -H $host -p $port` #if [[ "$res" =~ "OK" ]];then #nc -w 3 $host $port < /dev/null > /dev/null 2>&1 nc -w 3 -z $host $port > /dev/null 2>&1 if [ $? = 0 ] ; then echo "$cur_datetime 端口$port listening,status is ok..." | tee -a $homeDir/port_status.log exit 0 else echo "$cur_datetime Error 端口$port closed,status is failed..." | tee -a $homeDir/port_status.log if [ "$alert" = "True" ];then curl -s -X POST -H "'Content-type':'application/json'" -d '{ "msgtype": "text", "text": { "content": "端口未启动 \r 端口: '$port' \r 服务器: '$host' \r 时间: '$cur_datetime'", "mentioned_list":["wangqing",""], "mentioned_mobile_list":["",""] } }' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=$userkey fi exit 1 fi
用一个例子来演示会更加清晰
分类:
shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
2018-01-09 复习配置 - 逻辑链条 - zabbix items 配置 修改zabbix表结构 - 不错的文档
2018-01-09 被攻击后排查的过程
2018-01-09 centos6.4安装 zabbix agent
2018-01-09 复习zabbix配置agent过程