老师你好我想写一个脚本监控一些ip是否某个端口开放,能连通。我测试能连通的ip能输出,但不能连通的ip程序就不动了,请老师帮忙看看。如何写这个逻辑
来源:2-5 退出状态码
慕码人6397186
2020-10-08
#!/bin/bash
i=1
for ip in $( cat ./ip.txt )
do
result=`echo -e "\n" | telnet ${ip} 9151 2>/dev/null | grep Connected | wc -l`
# echo -e "\n" | wget 168.119.103.115:9151 2>/dev/null | grep connected | wc -l
# echo -e "\n" | telnet 168.119.103.115 9151 2>/dev/null | grep Connected | wc -l
if [ $result -ge 1 ]; then
echo "node$i $ip:9151 can be connected."
else
echo "node$i $ip:9151 cannot be connected."
fi
((i+=1))
done
exit 0
写回答
1回答
-
酷田
2020-10-26
可以通过获取echo "" | telnet localhost 8088的指令返回结果关键字来判定端口是否通,
脚本内容如下:
#!/bin/bash
#
port_list="8088 443 1908 1234 8761 18080"
for i in $port_list
do
echo "" | telnet localhost $i | grep "Escape character" &> /dev/null
if [ $? -eq 0 ];then
echo "$i is connected"
else
echo "$i is not connected"
fi
done
012022-04-26
相似问题
请问shell如何准确获取本机ip
回答 1
子shell问题
回答 1