Contents

Testing remote TCP port

Contents
  1. Update nodes.txt with the list of hosts or IP address
  2. Run the below snippet to check the open port 22 for the list in nodes.txt
  3. Change the port to check different port.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
exec 3<nodes.txt

while read -u3 host; do
port=22
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
     echo "$host $port is open"
else
     echo "$host $port is closed"
#     exit 1 # To force fail result in ShellScript
fi
done