Friday, July 20, 2012

How To Test If A TCP Port Is Open on UNIX


Sometimes we need to check if a certain TCP port is open. The following are some ways to do it.

$ netstat -an | grep 5100
      *.5100               *.*  0      0 4000000      0 LISTEN
      *.5100               *.*  0      0 4000000      0 LISTEN
      *.5100               *.*  0      0 4000000      0 LISTEN

SUCCESS:
$ echo | telnet server_name 5100
Trying 100.10.10.10...
Connected to server_name.
Escape character is '^]'.
Connection to server_name closed by foreign host.

FAIL:
$ telnet  server_name 5100
Trying 100.10.10.10...
telnet: Unable to connect to remote host: Connection refused