shell习题第6题:监听80端口

【题目要求】

写一个脚本,判断本机的80端口(加入服务为httpd)是否开启,如果开启就什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知相关人员

【核心要点】

检测80端口使用nmap -p 80 127.0.0.1 或者 netstat -lntp | grep -w 80

重启httpd的相关命令

发邮件脚本依然使用mail.py

【脚本】

#!/bin/bash

m=123@123.com
while :
do
    n=`netstat -lntp | grep ':80 ' | wc -l`
    if [ $n -eq 0 ]; then
        /usr/local/apache2/bin/apachectl -k restart 2> /tmp/apache.err
        python mail.py $m "80端口关闭" "已经重启httpd服务"
        pn=`pgrep -l httpd | wc -l`

        if [ $pn -eq 0 ]; then
            python mail.py $m "httpd重启失败" "`head -1 /tmp/apache.err`"
        fi

    fi
    sleep 30 
done
原文地址:https://www.cnblogs.com/dingzp/p/10768464.html