端口监控脚本:touch 1.sh
#!/bin/bash ADDR=10.1.25.2 TMPSTR=`ping ${ADDR} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'` port=443 check_ip=`nmap $TMPSTR -p $port|grep open|wc -l` starttime=$(date +%Y-%m-%d\ %H:%M:%S) echo $starttime if [ $check_ip -eq 0 ];then echo "${starttime} ${TMPSTR}:${port} DEAD!!!" >>error.txt fi echo "${starttime} ${TMPSTR}:${port} OK" >>success.txt
如果需要每分钟检测一次 将 */1 * * * * /bin/bash 1.sh 加入crontab即可 如果需要秒级监控 则需要第二个脚本配合使用
touch 2.sh
#!/bin/bash step=2 for (( i = 0; i < 60; i=(i+step) )); do $(/bin/bash '第一个脚本的绝对路径') sleep $step done exit 0
然后再将*/1 * * * * /bin/bash 2.sh 加入crontab即可
监控输出效果如下
