how to test a port with bash

Knowledge Base - How To

To test a port on a server

timeout X_seconds bash -c 'cat < /dev/null > /dev/tcp/server_ip/port_number'; echo $?

timeout 10 bash -c 'cat < /dev/null > /dev/tcp/10.33.20.217/22'; echo $? 0

Where "0" is success as per:

Connection successful:

$ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/80' $ echo $? 0

Connection failure prior to the timeout

$ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/sfsfdfdff.com/80' bash: sfsfdfdff.com: Name or service not known bash: /dev/tcp/sfsfdfdff.com/80: Invalid argument $ echo $? 1

Connection not established by the timeout

$ timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/81' $ echo $? 124