首页 > 文章列表 > Linux nc命令如何使用

Linux nc命令如何使用

linux
175 2023-06-06

Linux nc命令如何使用

一、命令简介

nc是netcat的简写,是一个功能强大的网络工具,有着网络界的瑞士军刀美誉。nc命令在linux系统中实际命令是ncat,nc是软连接到ncat。nc命令的主要作用如下:

实现任意TCP/UDP端口的侦听,nc可以作为server以TCP或UDP方式侦听指定端口端口的扫描,nc可以作为client发起TCP或UDP连接机器之间传输文件机器之间网络测速

nc如果找不到nc命令可以使用yum install -y nc安装,其中centos6系统安装的是nc-1.84-24.el6.x86_64,centos7安装的是nmap-ncat-6.40-19.el7.x86_64。

二、使用示例

1、验证服务器端口是否通

如下验证172.16.7.78服务器的80端口通,81端口不通。

2、拷贝文件

首先在文件接收终端test2机器上激活nc监听

然后在文件发送终端test1机器上发送文件

在test2上检查文件是否已成功接收

3、终端之间通信聊天

test1主机上启动nc监听,ctrl+C中断通信。

test2上连接监听,ctrl+C中断通信。

4、端口扫描

端口扫描,通的端口返回succeeded,不通的端口返回refused。此扫描基于nc-1.84-24.el6.x86_64。

[root@test1 /]# nc -v -w 1 172.16.7.78 -z 22-81

Connection to 172.16.7.78 22 port [tcp/ssh] succeeded!

nc: connect to 172.16.7.78 port 23 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 24 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 25 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 26 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 27 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 28 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 29 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 30 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 31 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 32 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 33 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 34 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 35 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 36 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 37 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 38 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 39 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 40 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 41 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 42 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 43 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 44 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 45 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 46 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 47 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 48 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 49 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 50 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 51 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 52 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 53 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 54 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 55 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 56 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 57 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 58 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 59 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 60 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 61 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 62 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 63 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 64 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 65 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 66 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 67 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 68 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 69 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 70 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 71 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 72 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 73 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 74 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 75 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 76 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 77 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 78 (tcp) failed: Connection refused

nc: connect to 172.16.7.78 port 79 (tcp) failed: Connection refused

Connection to 172.16.7.78 80 port [tcp/http] succeeded!

nc: connect to 172.16.7.78 port 81 (tcp) failed: Connection refused

5、验证UDP端口

[root@test1 ~]# nc -uvz 192.168.0.125 111

Ncat: Version 7.50 ( https://nmap.org/ncat )

Ncat: Connected to 192.168.0.125:111.

Ncat: UDP packet sent successfully

Ncat: 1 bytes sent, 0 bytes received in 2.04 seconds.

6、测速网速

测速网速需要结合dstat命令查看。

安装dstat命令

[root@test1 ~]# yum install -y dstat

[root@test2 ~]# yum install -y dstat

test1主机上监听端口

[root@test1 ~]# nc -l 33333 >/dev/null

test2主机上发送数据,全0数据

[root@test2 ~]# nc 192.168.0.124 33333 </dev/zero

查看流量

[root@test1 ~]# dstat

[root@test2 ~]# dstat

三、使用语法及参数说明

1、使用语法

用法:ncat [options] [hostname] [port]

2、参数说明

参数参数说明
-4Use IPv4 only
-6Use IPv6 only
-U, --unixsock仅使用Unix域套接字
-C, --crlf将crlf用于EOL序列
-c, --sh-exec <command>通过/bin/sh执行给定的命令
-e, --exec <command>执行给定的命令
&ndash;lua-exec <filename>执行给定的lua脚本
-g hop1[,hop2,&hellip;]松散源路由跃点(最多8个)
-G <n>松散源路由跃点指针(4,8,12,&hellip;)
-m, --max-conns <n>最大同时连接数
-h, --help帮助显示此帮助屏幕
-d, --delay <time>读/写之间的延迟
-o, --output <filename>将会话数据转储到文件
-x, --hex-dump <filename>将会话数据作为十六进制转储到文件
-i, --idle-timeout <time>空闲读/写超时
-p, --source-port port指定要使用的源端口
-s, --source addr指定要使用的源地址(不影响-l)
-l, --listen绑定并侦听传入连接
-k, --keep-open在侦听模式下接受多个连接
-n, --nodns不通过DNS解析主机名
-t, --telnet应答telnet协商
-u, --udp使用udp而不是默认TCP
&ndash;sctp使用sctp而不是默认的TCP
-v, --verbose设置详细级别(可以多次使用)
-w, --wait <time>连接超时时间,单位秒
-z仅报告连接状态
&ndash;append-output追加而不是重击指定的输出文件
&ndash;send-only忽略接收;退出EOF
&ndash;recv-only从不发送任何东西
&ndash;allow给定主机连接到Ncat
&ndash;allowfile允许连接到Ncat的主机的文件
&ndash;deny给定主机连接到Ncat
&ndash;denyfile拒绝连接到Ncat的主机文件
&ndash;broker启用Ncat的连接代理模式
&ndash;chat开始一个简单的Ncat聊天服务器
&ndash;proxy <addr[:port]>指定要通过代理的主机地址
&ndash;proxy-type <type>指定代理类型(“http”或“socks4”或“socks5”)
&ndash;proxy-auth <auth>通过HTTP或SOCKS代理服务器进行身份验证
&ndash;ssl使用ssl连接或侦听
&ndash;ssl-cert指定用于侦听的ssl证书文件(PEM)
&ndash;ssl-key指定用于侦听的ssl私钥(PEM)
&ndash;ssl-verify证书的信任和域名
&ndash;ssl-trustfile包含可信ssl证书的PEM文件
&ndash;ssl-ciphersCipherlist包含要使用的ssl密码
&ndash;version查看命令版本