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

2023-04-25,,

【题目要求】

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

【核心要点】

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

重启httpd的相关命令

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

【脚本】

#!/bin/bash

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

shell习题第6题:监听80端口的相关教程结束。

《shell习题第6题:监听80端口.doc》

下载本文的Word格式文档,以方便收藏与打印。