Linux之nc命令

关山初度尘未洗,策马扬鞭再奋蹄!这篇文章主要讲述Linux之nc命令相关的知识,希望能为你提供帮助。
Centos安装nc

> yum -y install nc

Ubuntu安装nc
> apt-get install netcat

Mac安装nc
> brew install netcat

命令格式
nc [-hlnruz][-g& lt; 网关...& gt; ][-G& lt; 指向器数目& gt; ][-i& lt; 延迟秒数& gt; ][-o& lt; 输出文件& gt; ][-p& lt; 通信端口& gt; ]< br/> [-s& lt; 来源位址& gt; ][-v...][-w& lt; 超时秒数& gt; ][主机名称][通信端口...]
命令选项
  • -g< 网关> 设置路由器跃程通信网关,最多可设置8个。
  • -G< 指向器数目> 设置来源路由指向器,其数值为4的倍数。
  • -h 在线帮助。
  • -i< 延迟秒数> 设置时间间隔,以便传送信息及扫描通信端口。
  • -l 使用监听模式,管控传入的资料。
  • -n 直接使用IP地址,而不通过域名服务器。
  • -o< 输出文件> 指定文件名称,把往来传输的数据以16进制字码倾倒成该文件保存。
  • -p< 通信端口> 设置本地主机使用的通信端口。
  • -r 乱数指定本地与远端主机的通信端口。
  • -s< 来源位址> 设置本地主机送出数据包的IP地址。
  • -u 使用UDP传输协议。
  • -v 显示指令执行过程。
  • -w< 超时秒数> 设置等待连线的时间。
  • -z 使用0输入/输出模式,只在扫描通信端口时使用。
TCP端口扫描
> nc -v -z-w2rumenz.com20-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 availableConnection 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

UDP端口扫描
> 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 availableConnection 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 availableConnection 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 availableConnection 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 availableConnection 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 availableConnection 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 availableConnection 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 availableConnection to json.im port 80 [tcp/http] succeeded!

建立 C/S 聊天室
模拟 Server 端
> nc -v -l 127.0.0.1 8086

> nc -v -p 8085 localhost 8086

发送文件
Server 端接收文件
> nc -l 127.0.0.1 8086 > s.txt

Client 端发送文件:
> nc 127.0.0.1 8086 < c.txt

支持 UDP 和 IPv6 连接
Server 端:
> nc -u -6 -l localhost 8086

Client 端
> nc -u -6 localhost 8086

【Linux之nc命令】原文链接:https://rumenz.com/rumenbiji/linux-nc.html
微信公众号:入门小站

    推荐阅读