Bash 检查 IP/端口是否开启

Bash 检查 IP/端口是否开启

我尝试使用 bash 脚本通过 netstat 检查 IP/端口的状态。不幸的是,它不起作用...我得到了退出 1,即使它应该是退出 0。

RUNBOT=$(netstat -tulpen |grep ${IP}:${PORT} | wc -l)

    if [ "${RUNBOT} = "1" ]; then
            echo "Bot ${IP}:${PORT} is running!"        
        exit 0
    else
            echo "Bot ${IP}:${PORT} is NOT running!"
        exit 1
    fi

答案1

lsof -ni @${IP}:${PORT} >/dev/null
b=$?
if [ $b = 0 ]; then
            echo "Bot ${IP}:${PORT} is running!"        
        exit 0
    else
            echo "Bot ${IP}:${PORT} is NOT running!"
        exit 1
fi

相关内容