使用脚本批量Telnet测试端口是否连通,使用telnet:
telnets.sh:
#!/bin/bash
check_telnet(){
for ip_port in $(cat ip_info|grep -v '^#')
do
CHECK_PORT=$(echo $ip_port|awk -F: '{print $2}')
CHECK_IP=$(echo $ip_port|awk -F: '{print $1}')
echo -e "\n"| telnet $CHECK_IP $CHECK_PORT |grep "Connected to\|Escape character" >/dev/null
if [ $? -eq 0 ];then
echo "$LOCALIP result $CHECK_IP : $CHECK_PORT connected"
else
echo "$LOCALIP result $CHECK_IP : $CHECK_PORT can not Connected"
fi
done
}
check_telnet > result.log
vim ip_info
10.192.219.12:5888
10.192.219.12:22
10.192.155.2:22
10.192.155.2:3306
10.192.155.1:3306
cat result.log
result 10.192.219.12 : 5888 can not Connected
result 10.192.219.12 : 22 connected
result 10.192.155.2 : 22 connected
result 10.192.155.2 : 3306 can not Connected
result 10.192.155.1 : 3306 connected
有时候telnet命令没有,系统只提供curl时:
telnets-curl.sh
#!/bin/bash
check_telnet(){
for ip_port in $(cat ip_info|grep -v '^#')
do
CHECK_PORT=$(echo $ip_port|awk -F: '{print $2}')
CHECK_IP=$(echo $ip_port|awk -F: '{print $1}')
echo -e "\n\n\n"| curl --connect-timeout 5 -s $CHECK_IP:$CHECK_PORT >/dev/null
if [ $? -eq 0 ];then
echo "$LOCALIP result $CHECK_IP : $CHECK_PORT connected"
else
echo "$LOCALIP result $CHECK_IP : $CHECK_PORT can not Connected"
fi
done
}
check_telnet > result1.log
标签:ip,10.192,端口,Telnet,echo,PORT,result,Curl,CHECK From: https://blog.51cto.com/lenglingx/6390786