shell 检测服务端口状态
程序员文章站
2022-06-04 19:47:49
...
#!/bin/bash
port_array=(3306 27017 80 10080)
service_array=(mysql mongod nginx php-fpm)
for(( i=0;i<${#port_array[@]};i++)) do
time=$(date "+%Y-%m-%d %H:%M:%S")
port=${port_array[i]};
service_name=${service_array[i]};
port_status=`netstat -nlt|grep ${port_array[i]}|wc -l`
if [ $port_status -lt 1 ]
then
echo -e "\033[31m[FAIL] $time $service_name:$port\033[0m"
else
echo -e "\033[32m[INFO] $time $service_name:$port\033[0m"
fi
done;
执行效果如下: