![Bash 检查 IP/端口是否开启](https://linux22.com/image/724921/Bash%20%E6%A3%80%E6%9F%A5%20IP%2F%E7%AB%AF%E5%8F%A3%E6%98%AF%E5%90%A6%E5%BC%80%E5%90%AF.png)
我尝试使用 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