Linux常用命令-ncat

命令

ncat

描述

Concatenate and redirect sockets
连接和重定向sockets,网络测试工具,是nmap的一个子工具

用法

1
ncat [OPTIONS...] [hostname] [port]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Options:
默认的<time>时间单位为秒s,也可以指定ms,s,m,h. 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
-4 使用IPv4
-6 使用IPv6
-U, --unixsock 使用sockets
-C, --crlf Use CRLF for EOL sequence
-c, --sh-exec <command> 通过/bin/sh执行指定命令
-e, --exec <command> 执行指定命令
--lua-exec <filename> 执行lua脚本
-g hop1[,hop2,...] Loose source routing hop points (8 max)
-G <n> Loose source routing hop pointer (4, 8, 12, ...)
-m, --max-conns <n> 最大并发连接数
-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 不解析主机名
-t, --telnet 响应telnet请求
-u, --udp 使用UDP协议,默认为TCP协议
--sctp 使用SCTP协议
-v, --verbose 显示详细信息,可以多次重复使用,如-vvv
-w, --wait <time> 连接超时时间
-z 零I/O模式,仅报告连接状态,可用于端口探测
--append-output 追加输出到指定文件
--send-only Only send data, ignoring received; quit on EOF
--recv-only Only receive data, never send anything
--allow Allow only given hosts to connect to Ncat
--allowfile A file of hosts allowed to connect to Ncat
--deny Deny given hosts from connecting to Ncat
--denyfile A file of hosts denied from connecting to Ncat
--broker Enable Ncat's connection brokering mode
--chat Start a simple Ncat chat server
--proxy <addr[:port]> Specify address of host to proxy through
--proxy-type <type> Specify proxy type ("http" or "socks4" or "socks5")
--proxy-auth <auth> Authenticate with HTTP or SOCKS proxy server
--ssl Connect or listen with SSL
--ssl-cert Specify SSL certificate file (PEM) for listening
--ssl-key Specify SSL private key (PEM) for listening
--ssl-verify Verify trust and domain name of certificates
--ssl-trustfile PEM file containing trusted SSL certificates
--ssl-ciphers Cipherlist containing SSL ciphers to use
--version Display Ncat's version information and exit

注意

nc命令是ncat的软链接,ncat是nmap的一个子工具,软件包名称为nmap-ncat,安装nmap会自动安装ncat

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ ncat --version
Ncat: Version 7.50 ( https://nmap.org/ncat )

# -z 端口探测,如下80和443连接正常,8080连接失败
$ ncat -zv www.baidu.com 80
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 110.242.68.3:80.
Ncat: 0 bytes sent, 0 bytes received in 0.04 seconds.
$ ncat -zv www.baidu.com 443
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 110.242.68.3:443.
Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.
$ ncat -zv www.baidu.com 8080
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connection to 110.242.68.4 failed: Connection timed out.
Ncat: Trying next address...
Ncat: Connection timed out.

# -l 监听端口,默认为tcp端口,即启用服务端
$ ncat -l 8080
# -u 指定udp端口
$ ncat -l -u 1234

# 探测udp端口,nc命令显示succeeded表示连接正常,显示为空则表示连接失败
$ nc -uzv 192.168.80.10 1234
Connection to 192.168.80.10 1234 port [udp/*] succeeded!
# 连接成功
$ ncat -uzv 192.168.80.10 1234
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.80.10:1234.
Ncat: UDP packet sent successfully
Ncat: 1 bytes sent, 0 bytes received in 2.01 seconds.
# 连接失败
$ ncat -uzv 192.168.80.10 1234
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.80.10:1234.
Ncat: Connection refused.