用于设置路由器,是网络工具中的瑞士军刀
nc命令 全称netcat,用于设置路由器。它能通过 TCP 和 UDP 在网络中读写数据。通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它。使用 netcat 命令所能完成的事情令人惊讶。
nc
> yum -y install nc
nc
> apt-get install netcat
nc
> brew install netcat
nc [-hlnruz][-g<网关...>][-G<指向器数目>][-i<延迟秒数>][-o<输出文件>][-p<通信端口>] [-s<来源位址>][-v...][-w<超时秒数>][主机名称][通信端口...]
> nc -v -z -w2 rumenz.com 20-30
nc: connectx to rumenz.com port 20 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 21 (tcp) failed: Connection refused
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif en0
src 192.168.0.105 port 57984
dst 203.195.152.245 port 22
rank info not available
TCP aux info available
Connection to rumenz.com port 22 [tcp/ssh] succeeded!
nc: connectx to rumenz.com port 23 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 24 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 25 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 26 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 27 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 28 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 29 (tcp) failed: Connection refused
nc: connectx to rumenz.com port 30 (tcp) failed: Connection refused
20-30
是端口范围
> nc -v -z -w2 -u rumenz.com 20-25
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.0.105 port 63603
dst 203.195.152.245 port 20
rank info not available
Connection to rumenz.com port 20 [udp/ftp-data] succeeded!
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.0.105 port 51642
dst 203.195.152.245 port 21
rank info not available
Connection to rumenz.com port 21 [udp/ftp] succeeded!
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.0.105 port 58180
dst 203.195.152.245 port 22
rank info not available
Connection to rumenz.com port 22 [udp/ssh] succeeded!
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.0.105 port 57409
dst 203.195.152.245 port 23
rank info not available
Connection to rumenz.com port 23 [udp/telnet] succeeded!
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.0.105 port 59767
dst 203.195.152.245 port 24
rank info not available
Connection to rumenz.com port 24 [udp/*] succeeded!
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif (null)
src 192.168.0.105 port 58696
dst 203.195.152.245 port 25
rank info not available
Connection to rumenz.com port 25 [udp/smtp] succeeded!
> nc -v json.im 80
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif en0
src 192.168.0.105 port 58683
dst 150.109.147.28 port 80
rank info not available
TCP aux info available
Connection to json.im port 80 [tcp/http] succeeded!
nc 的本质是在两台机器之间建立连接,之后就可以基于这个连接做很多事情,数据传输是其中一个最为基本的。我们下面就使用 nc 来建立一个 C/S 的聊天室。
> nc -v -l 127.0.0.1 8086
监听
8086
端口
> nc -v -p 8085 localhost 8086
-p : 指定源端口
连上之后,Client 和 Server 端就可以相互聊天了。
nc 不仅可以发送消息,还可发送文件。
假设服务端有一个 s.txt 的空文件,而客户端有一个 c.txt 文件,含有数据:rumenz.com。
> nc -l 127.0.0.1 8086 > s.txt
> nc 127.0.0.1 8086 < c.txt
然后
s.txt
就会有rumenz.com
的数据。除了可以发送文件,nc 也可以发送目录,只需要将目录压缩发送即可。
nc 默认使用 TCP 和 IPv4 协议建立连接,我们可以使用参数 -u 和 -6 参数来分别使用建立 UDP 和 IPv6 连接。
> nc -u -6 -l localhost 8086
> nc -u -6 localhost 8086
原文链接:https://rumenz.com/rumenbiji/linux-nc.html