An existing tool that does this is nmap
, a port scanner:
$ nmap -sT -p 22 192.168.1.1-5
Starting Nmap 7.95 ( https://nmap.org ) at 2024-12-12 22:52 Eastern Standard Time
Nmap scan report for 192.168.1.1
Host is up (0.0039s latency).
PORT STATE SERVICE
22/tcp open ssh
MAC Address: 74:AC:B9:12:E1:93 (Ubiquiti)
Nmap done: 5 IP addresses (1 host up) scanned in 1.47 seconds
-sT
indicates a TCP portscan - it will SYN, ACK, SYN/ACK, ACK and then RST the connection.
-p 22
indicates port 22 will be tested.
It can scan individual IPs, IP ranges (as shown above), IP CIDR blocks, or any combination of these three.
It also supports XML and grep-able output, like such:
$ nmap -sT -p 22 -oG - 192.168.1.1,3,5
# Nmap 7.95 scan initiated Thu Dec 12 22:59:14 2024 as: nmap -sT -p 22 -oG - 192.168.1.1,3,5
Host: 192.168.1.1 () Status: Up
Host: 192.168.1.1 () Ports: 22/open/tcp//ssh///
# Nmap done at Thu Dec 12 22:59:16 2024 -- 3 IP addresses (1 host up) scanned in 1.45 seconds