Wi-Fizzle.com - Putting the fizzle in Wi-Fi since 2005 .. (yes, this was a poor choice for a domain name)

<div class="news_item">#458<div class="news_title">HOWTO: Command-line test for whether or not a TCP port is open

Posted by dandriff on Thursday August 30, 2012@01:13PM

HOWTO: Command-line test for whether or not a TCP port is open

Usually I just use `telnet <host_or_ip> <port>' to see if a port is open, but this doesn't lend itself to automation. One way to accomplish this automatically is

 nc -z <host_or_ip> <port> 1>/dev/null 2>&1; result=$?;
 if [ $result -eq 0 ]; then
     echo 'the port is open for tcp connections'
 else
     echo 'the port was closed'
 fi

If the port is open, the exit status will be `0', otherwise it will be `1'.

Reference: How to test if remote TCP port is opened from Shell script?