How to check if ports are open on an array of servers

Okay now there is a whole bunch of ways you can do this. This is just the way I played around with to save myself a bunch of time, using NCAT. Also previously known as NETCAT.

1.Ensure your Jumphost can ssh to all your newely deployed machines. Either you will use a root password or ssh key of some sort.

2.You will also need to install ncat
a.Yum install nmap-ncat (redhat/centos)
Note (ensure you have this install on all the new servers) 

3.Open your editor and copy and paste this script below and save the file
b.Vi & save
c.Chmod +x (change permissioned to executable) – this will check to see if your new server can talk to all the hosts below and check to see if those ports are up or down on each



host=”nick1 nick2 nick3 nick4″

for host in $host; do

for port in 22 53 67 68


if ncat -z $host $port


echo port $port $host is up


echo port $port $host is down





4.Next you want create an array for your for loop to cycle through and check if all those servers can communicate with those machine and ports
d.Create a file called servers
i.Vi servers
ii.Add a bunch of hosts in a single column






e.Save the file servers


5.Now what were going to is have a for loop cycle through the list by logging into each host running that script and outputting the results to a file for us to look at.


6.Run the following below check the servers and see if each server can communicate with the hosts and ports necessary. If you see the are down. Then you will need to check the firewalls to see why the host is unable to communicate.

 for HOST in $(cat server.txt) ; do ssh root@$HOST “bash -s” < ; echo $HOST ; done 2>&1 | tee -a port.status

Note: the file port.status will be created on the jump host and you can simply look through to see if any ports were down on whichever hosts.


This is what the script looks like on one host if its working properly

[root@nick ~]# ./

port 22 is up

port 53 is down

port 67 is down

port 68 is down


This is what it will look like when you run against your array of new hosts from your jumpbox

[root@nick ~]# for HOST in $(cat servers.txt) ; do ssh root@$HOST “bash -s” < ; echo $HOST ; done

root@’s password:

port 22 nick1 is up

port 53 nick1 is down

port 67 nick1 is down

port 68 nick1 is down

port 22 nick2 is up

port 53 nick2 is down

port 67 nick2 is down

port 68 nick2 is down

Leave a Reply

Your email address will not be published. Required fields are marked *