nc】 netcat 网络界的瑞士军刀【linux】


安装:apt-get install netcat

nc的常用的几个参数如下所列:

-l 用于指定nc将处于侦听模式。指定该参数,则意味着nc被当作server,侦听并接受连接,而非向其它地址发起连接。

-p 暂未用到(老版本的nc可能需要在端口号前加-p参数,下面测试环境是centos6.6,nc版本是nc-1.84,未用到-p参数)

-s 指定发送数据的源IP地址,适用于多网卡机

-u 指定nc使用UDP协议,默认为TCP

-v 输出交互或出错信息,新手调试时尤为有用

-w 超时秒数,后面跟数字

-z 表示zero,表示扫描时不发送任何数据

tcp监听

nc -l -p 9999

作为客户端工具进行端口探测

-v可视化,-z扫描时不发送数据,-w超时几秒,后面跟数字

nc -vz -w 2 10.0.1.161 9999

nc -vzw 2 10.0.1.161 9998-9999

udp监听

nc -ul -p 9998

udp端口扫描

nc -vuz 10.0.1.161 1-1000

nc传输文件

nc -l -p 9995 > file # 端口接收到文件存为file

nc 10.0.0.1 9995 < file # 发送文件file到ip和端口

nc -l -p 9992 < file # 准备发送文件file到端口9992

nc 10.0.0.1 9992 > file # 连接到指定ip和端口,将接收到的内容存为file

传输目录

nc -l -p 9995 | tar xzvf - # 等待接收

tar cfz - * | nc 10.0.0.1 9995 # 把当前目录下的所有文件打包为-,然后nc发送给目标

网速测试(通过dstat等工具观察速率)

nc -l 9991 >/dev/null # 目标机上等待接收数据,但不保存(用于观察两者之间的速率)

nc 10.0.0.1 9991 < /dev/zero # 给目标主机发送无限的0

打开到主机host.example.com的TCP端口42进行连接,使用端口31337作为源端口,超时时间为5秒:

nc -p 31337 -w 5 host.example.com 42

网络克隆硬盘(非mount系统上进行)

nc -l -p 1234 | dd of=/dev/sda # 目标,监听,写入

dd if=/dev/sda | nc 10.0.0.1 1234 # 源:读取,发送

打开主机UDP端口53进行连接

nc -u host.example.com 53

打开到主机host.example.com端口42的TCP连接,使用10.1.2.3作为连接本地端的IP

nc -s 10.1.2.3 host.example.com 42

在UNIX域流套接字上创建和侦听

nc -lU /var/tmp/dsocket

连接到主机host.example.com的端口42,通过HTTP代理在10.2.3.4端口8080访问。ssh(1)也可以使用这个示例;

有关更多信息,请参阅ssh_config(5)中的ProxyCommand指令。

nc -x10.2.3.4:8080 -Xconnect host.example.com 42

同样的例子,如果代理需要,这一次使用用户名“ruser”启用代理身份验证

nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42

使用-s选项为测试选择源IP

nc -zv -s source_IP target_IP Port

nc】 【linux】


To open a TCP connection to port 42 of host.example.com, using port 31337 as the source port, with a timeout of 5 seconds:

nc -p 31337 -w 5 host.example.com 42

To open a UDP connection to port 53 of host.example.com:

nc -u host.example.com 53

To open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the IP for the local end of the connection:

nc -s 10.1.2.3 host.example.com 42

To create and listen on a UNIX-domain stream socket:

nc -lU /var/tmp/dsocket

To connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, port 8080. This example could also be used by ssh(1); see the ProxyCommand directive in ssh_config(5) for more information.

nc -x10.2.3.4:8080 -Xconnect host.example.com 42

The same example again, this time enabling proxy authentication with username "ruser" if the proxy requires it:

nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42

To choose the source IP for the testing using the -s option

nc -zv -s source_IP target_IP Port


腾图小抄 SCWY.net v0.03 小抄561条 自2022-01-02访问368652次