Simple test if TCP port is open
This blog post was published 6 years ago and may or may not have aged well.
While reading please keep in mind that it may no longer be accurate or even relevant.
There are other more complicated tools to achieve the same (like nmap
whose manpage makes your head spin), but this is a very simple solution using netcat
:
nc -zv 127.0.0.1 80
To programmatically evaluate the result, use the standard Bash $?
variable. It will be set to 0 if the port was open, or 1 if the port was closed.