quick check of open port

Posted by shantanuo on Server Fault See other posts from Server Fault or by shantanuo
Published on 2012-03-21T10:11:18Z Indexed on 2012/03/21 11:31 UTC
Read the original article Hit count: 266

Filed under:
|

The following is working as expected. (do not want to use nmap) I need to use nc (or any other built-in centOS) command in shell script to check the port 6379 of a remote server. I want the script to exit quickly if no response received in less than 1 second. But it seems that nc will wait for too long before quitting with exit code of 1

How do I "quickly" check if the port is listening?

# time nc -z 1.2.3.4 1234

real    0m21.001s
user    0m0.000s
sys 0m0.000s

# echo $?
1

# time nc -z 1.2.3.4 6379
Connection to 1.2.3.4 6379 port [tcp/*] succeeded!

real    0m0.272s
user    0m0.000s
sys 0m0.008s

# echo $?
0

© Server Fault or respective owner

Related posts about ping

Related posts about netcat