背景
一般在进行网络访问不通的时候需要进行端口测试,在windows环境下,可以使用telnet命令进行检测:
1 | telnet www.baidu.com 443 |
telnet检测仅适用于tcp协议的端口检测,对于udp协议的则毫无办法。
在Liunx和macOS环境下,默认是没有安装的telnet的,但是有nc这工具。试用了一下发现检测效果更佳。
NC网络检测实战
tcp检测
1
2
3
4
5
6
7
8
9
10
11zhengkaideMacBook-Pro:~ zhengk$ nc -z -v 220.181.38.148 80
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif en0
src 192.168.31.68 port 64654
dst 220.181.38.148 port 80
rank info not available
TCP aux info available
Connection to 220.181.38.148 port 80 [tcp/http] succeeded!udp检测
1
2
3
4
5
6
7
8
9
10zhengkaideMacBook-Pro:~ zhengk$ nc -u -z -v 220.181.38.148 80
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.31.68 port 60536
dst 220.181.38.148 port 80
rank info not available
Connection to 220.181.38.148 port 80 [udp/http] succeeded!范围端口检测
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16zhengkaideMacBook-Pro:~ zhengk$ nc -z -v -G 2 220.181.38.148 75-80
nc: connectx to 220.181.38.148 port 75 (tcp) failed: Operation timed out
nc: connectx to 220.181.38.148 port 76 (tcp) failed: Operation timed out
nc: connectx to 220.181.38.148 port 77 (tcp) failed: Operation timed out
nc: connectx to 220.181.38.148 port 78 (tcp) failed: Operation timed out
nc: connectx to 220.181.38.148 port 79 (tcp) failed: Operation timed out
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif en0
src 192.168.31.68 port 64689
dst 220.181.38.148 port 80
rank info not available
TCP aux info available
Connection to 220.181.38.148 port 80 [tcp/http] succeeded!
扩展
在macOS的环境下,nc命令的使用参数与linux环境下的参数代表的并不一样,这需要特别注意,常用的几个参数如下:
-G
:用来指定判断超时的时间,比如-G 2
就指2秒就算超时。注意:在linux中,使用-w
参数来指定超时-v
:用于输出交互过程。-z
:在端口扫描的情况下使用,否则不会自动退出结束。
更多的参数配置可以使用-h
来获取帮助:
1 | zhengkaideMacBook-Pro:~ zhengk$ nc -h |