Check port connectivity in batches

Original link: https://blog.kelu.org/tech/2022/09/14/batch-telnet-ports.html

The easiest way, of course, is to use ansible.

However, some of the customer’s production environments did not use ansible, so they wrote a simple script to test it.

focus point:

  1. At the beginning, the IP address of the network card eth0 was taken.
  2. Save the ip_info file in the same directory and fill in ip:port in each line
 #!/bin/bash LOCALIP = ` ifconfig -a | grep eth0 -A 2|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}' |tr -d "addr:" ` 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 -e " $CHECK_IP \t $CHECK_PORT \t ok" else echo -e " $CHECK_IP \t $CHECK_PORT \t error!!!" fi done } check_telnet > result.log echo "========= $LOCALIP =============" cat result.log

This article is reprinted from: https://blog.kelu.org/tech/2022/09/14/batch-telnet-ports.html
This site is for inclusion only, and the copyright belongs to the original author.