Operations 7 min read

Automate Multi‑Server Port Scanning with Shell & Netcat

Learn how to use the netcat (nc) command together with simple shell scripts to efficiently check whether one or multiple ports are open across a list of servers, eliminating manual checks and speeding up network diagnostics for both single‑port and multi‑port scans.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Automate Multi‑Server Port Scanning with Shell & Netcat

Why automate port checks?

When managing a few servers you can manually test a port with the nc command, but in a cluster this becomes time‑consuming and error‑prone. A shell script combined with nc can probe any number of hosts and ports automatically.

About netcat (nc)

nc

(netcat) is a versatile networking utility that can read from and write to TCP or UDP connections. It supports three main modes: connect, listen, and relay, and its basic syntax is nc [options] host|IP port.

Scanning a single port on multiple servers

1. Create a file server-list.txt with one IP address per 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

2. Write a script port_scan.sh that loops over the list and runs nc -zvw3 $server 22 to test SSH (port 22).

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

3. Make the script executable and run it.

$ chmod +x port_scan.sh
$ 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!
... (output for each host)

Scanning multiple ports on multiple servers

1. Re‑use server-list.txt and create port-list.txt containing the ports to test, one per line (e.g., 22 and 80).

# cat port-list.txt
22
80

2. Write a script multiple_port_scan.sh with nested loops: the outer loop iterates over servers, the inner loop over ports.

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

3. Make it executable and run it.

$ chmod +x multiple_port_scan.sh
$ 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!
... (output for each host/port pair)

These scripts provide a quick, repeatable way to verify service availability across large server farms without manual intervention.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationLinuxShellport scanningNetwork Diagnosticsnetcat
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.