netcat

Test firewall port forwarding

I had a big problem with my voip bridge which is located behind my (OpenBSD pf based) firewall. To work correctly external port 5060 tcp and 10000-10100 udp must be redirected to my internal voip bridge. To verify that my configuration is working as supposed I swapped my voip bridge with a laptop with a nc listening on ports I want to check. The network settings on my laptop is of course setup identical to my voip bridge.

To test TCP connectivity on port 1234. Do the following on receiving host, target.

nc -l 1234

And the following on the source host, source.

echo hello | nc target 1234

To test a UDP port the same commands with an additional -u flag supplied should do it.

nc -u -l 1234

And the following on the source host, assuming target is localhost. Note that the target host must be fully qualified IP address to work with UDP. The reason is that nc sends a UDP packet on IPv6 instead of on IPv4. See UDP to localhost with netcat not working for more information. You may also add option -4 to restrict nc to IPv4.

echo hello | nc -u 127.0.0.1 1234        # OR
echo hello | nc -u -4 localhost 1234

References