It is often necessary to check a remote device to see if certain ports are open. For a quick & dirty test, use netcat (nc) in Terminal.

If you want to automate things a bit to reduce typing, add the following bash function to your ~/.bashrc file

portscan() {
echo 'Stealthed ports will timeout instead of failing'
read -p 'IP Address: ' ipaddr
read -p 'Port: ' portaddr
nc -zvw 3 "$ipaddr" "$portaddr"
}

To reload .bashrc after editing it, run:

source ~/.bashrc

Now run:

portscan 

from Terminal and you will be prompted for an IP Address or hostname along with a port to scan. If you want to scan a range of ports use a hyphen, i.e. 20-80 to scan all ports between 20 and 80.

To run netcat directly from terminal, you can use this command:

nc -z -v [IP-ADDRESS] 1-65535 2>&1 | grep -v 'Connection refused'

Pipe the output to the grep command using the -v option excludes any line that has “Connection refused” as a matched pattern. This will show all the ports (1-65535) that are open on the computer which are accessible by another machine on the network.

Leave a Reply

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