One way to test port survival under Mac and Linux [reproduced]

The most common way to test port survival is telnet

$telnet 127.0.0.1 80

However, this method has an inconvenience: after the connection is successful, it will enter the interactive mode, and you need to press  ctrl + ] and  q to exit.

Later, after searching on the Internet, I found an easy way to ask the gods nc

$nc -zv 127.0.0.1 80
Connection to 127.0.0.1 port 80 [tcp/arepa-cas] succeeded!    # 成功
nc: connectx to 127.0.0.1 port 80 (tcp) failed: Connection refused    # 失败

After the command is run, 1. It will not enter the interactive mode and return immediately; 2. The return value characteristics of success and failure are obvious, and it is very convenient to make judgments in the script.

Guess you like

Origin blog.csdn.net/sun124608666/article/details/105488642