Check connection with netcat nc

If you’d like to check if a port is listening (e.g. typical usage in a bash script), you can use the following line:

nc -w 1 -z $IP $PORT > /dev/null 2>&1

After running this command, you can check the return code.

If $? is 0, the connection is up. If it’s 1, it’s not working.