Check whether the port is Open on Multiple Remote Linux Systems using nc Command & shell script

We had recently written an article to check whether the port is open on the remote Linux server. It will help you to check for single server.If you want to check for multiple servers then  you can use any of the one following command such as

  • nc (netcat)
  • nmap and
  • telnet.

If you would like to check for 50+ servers then what will be the solution?

It’s not easy to check all servers by simple command, To overcome this situation, we had coded a small shell script using nc command that will allow us to scan any number of servers with given port.

If you are looking for a single server scan then you have multiple options, to know more about it. Feel free to navigate to the following URL

Check Whether A Port Is Open On The Remote Linux System?

There are two scripts available in this tutorial and both the scripts are very useful with different purpose, which you can easily understand by reading a head line.

Let us see few questions before you reading this article, just answer yourself

How to check, if a port is open on the remote Linux server?

How to check, if a port is open on the multiple remote Linux server?

How to check, if multiple ports are open on the multiple remote Linux server?

We will get more clarification of these questions after reading this article

What is nc (netcat) Command?

nc stands for netcat. Netcat is a simple Unix utility which reads and writes data across network connections, using TCP or UDP protocol.It is designed to be a reliable “back-end” tool that can be used directly or easily driven by other programs and scripts.

At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.

Netcat has three main modes of functionality. These are the connect mode, the listen mode, and the tunnel mode.

Common Syntax for nc (netcat):

$ nc [-options] [HostName or IP] [PortNumber]

How to check whether a Port is open on multiple remote Linux server?

Use the following shell script if you would like to check the given port is open on multiple remote Linux servers or not.

In my case, we are going to check whether the port 22 is open in the following remote servers or not? Make sure you have to update your servers list in the file instead of ours.

Make sure you have to update the servers list into server-list.txt file. Each server should be in separate line.

# cat server-list.txt
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7

Use the following script to achieve this.

# vi port_scan.sh

#!/bin/sh
for server in `more server-list.txt`
do
#echo $i
nc -zvw3 $server 22
done

Set an executable permission to port_scan.sh file.

$ chmod +x port_scan.sh

Finally run the script to achieve this.

# sh port_scan.sh

Connection to 192.168.1.2 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.3 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.4 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.5 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.6 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.7 22 port [tcp/ssh] succeeded!

How to check whether multiple ports are open on multiple remote Linux server?

Use the following script if you want to check the multiple ports in multiple servers.

In my case, we are going to check whether the port 22 and 80 is open or not in the given servers. Make sure you have to replace your required ports and servers name instead of ours.

Make sure you have to update the port lists into port-list.txt file. Each port should be in a separate line.

# cat port-list.txt
22
80

Make sure you have to update the servers list into server-list.txt file. Each server should be in separate line.

# cat server-list.txt
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7

Use the following script to achieve this.

# vi multiple_port_scan.sh

#!/bin/sh
for server in `more server-list.txt`
do
for port in `more port-list.txt`
do
#echo $server
nc -zvw3 $server $port
echo ""
done
done

Set an executable permission to multiple_port_scan.sh file.

$ chmod +x multiple_port_scan.sh

Finally run the script to achieve this.

# sh multiple_port_scan.sh
Connection to 192.168.1.2 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.2 80 port [tcp/http] succeeded!

Connection to 192.168.1.3 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.3 80 port [tcp/http] succeeded!

Connection to 192.168.1.4 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.4 80 port [tcp/http] succeeded!

Connection to 192.168.1.5 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.5 80 port [tcp/http] succeeded!

Connection to 192.168.1.6 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.6 80 port [tcp/http] succeeded!

Connection to 192.168.1.7 22 port [tcp/ssh] succeeded!
Connection to 192.168.1.7 80 port [tcp/http] succeeded!

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...