Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
netstat命令参数:
-a ##all,显示所有选项,默认不显示LISTEN相关
-n ##不做解析
-t ##tcp,仅显示tcp相关选项
-u ##udp,仅显示udp相关选项
-p ##进程名称
-e ##扩展信息
-l ##仅列出有在 Listen 的服务状态
-s ##按各个协议进行统计
-c ##每隔一个固定时间,执行该netstat命令
编写httpd监控脚本,要求可以输入start|stop|restart|status
#!/bin/bash
while true
do
echo -e "
\033[31m start 开启http服务 \033[0m
\033[32m stop 关闭http服务 \033[0m
\033[33m restart 重启http服务 \033[0m
\033[34m status 显示http服务的开启状态 \033[0m
\033[35m Q 退出系统 \033[0m
"
read -p "请输入选项:" char
case $char in
start)
if [ `systemctl status httpd | grep " Active" | awk '{print $2}'` == "active" ];then
echo "服务已经开启!"
else
systemctl start httpd
if [ `systemctl status httpd | grep " Active" | awk '{print $2}'` == "active" ];then
echo "服务开启成功!"
else
echo "服务开启失败!"
fi
fi
;;stop)
systemctl stop httpd
if [ `systemctl status httpd | grep " Active" | awk '{print $2}'` == "active" ];then
echo "服务关闭失败!"
else
echo "服务关闭成功!"
fi
;;
restart)
systemctl restart httpd
if [ `systemctl status httpd | grep " Active" | awk '{print $2}'` == "active" ];then
echo "服务重启成功!"
else
echo "服务重启失败!"
fi
;;
status)
systemctl status httpd &> /dev/null
echo "http服务当前的状态为:" `systemctl status httpd | grep " Active" | awk '{print $2}'`
;;
q|Q)
exit 0
;;
*)
echo "请输入正确的选项:"
;;
esac
done
脚本运行结果: