Netcat
Check port availability
Use netcat to get port information (preinstalled on Linux and macOS).
Check if a certain port is available via TCP connection:
$ nc -v -z <host_ip> <port>
In case, the port is actively used and open, a success message is reported, if not, a connection refused message appears.
Same for UDP connection:
$ nc -v -z -u <host_ip> <port>
To check for an available port on your local system, run:
$ nc -zv <host_ip> <port_from>-<port_until> 2>&1 | grep -m 1 failed
to receive the first unused port within the specified range.
Hint
Don’t assign port numbers 1 to 1024 for your services, as well-known services reserve those.
Alternative
You may also run open_ports.sh to receive information on a
port range (<from_port> until <to_port>):
$ ./open_ports <host_ip> <from_port> <to_port>
Send chat messages between two machines
Netcat is able to send simple chat messages between two machines. One machine acts as the server, other devices can connect to the server as client. All machines must have netcat installed.
On the server machine, create a netcat instance to listen to a specific port (here: 10001):
$ nc -n -v -l 10001
On the client side, create a connection to the specified port on the server (host):
$ nc -n -v <server_ip_address> 10001
Now, you may enter a message on either the server or any client machine and all connection machines will receive the message.