shell 检测服务端口状态

#!/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`
  
    service_process_num=`pgrep -l $service_name | wc -l`

    if [ $port_status -lt 1 ]
    then
        echo -e "\033[31m[FAIL] $time $service_process_num $service_name:$port\033[0m"
    else
        echo -e "\033[32m[INFO] $time $service_process_num $service_name:$port\033[0m"
    fi
done;

执行效果如下:

你可能感兴趣的:(Linux)