2024年1月12日

Linux之瑞士军刀nc

作者 高劲松

[XXX@hostname]# sudo nc –help
Ncat 7.50 ( https://nmap.org/ncat )
Usage: ncat [options] [hostname] [port]

Options taking a time assume seconds. Append ‘ms’ for milliseconds,
‘s’ for seconds, ‘m’ for minutes, or ‘h’ for hours (e.g. 500ms).
-4 Use IPv4 only
-6 Use IPv6 only
-U, –unixsock Use Unix domain sockets only
-C, –crlf Use CRLF for EOL sequence
-c, –sh-exec Executes the given command via /bin/sh
-e, –exec Executes the given command
–lua-exec Executes the given Lua script
-g hop1[,hop2,…] Loose source routing hop points (8 max)
-G Loose source routing hop pointer (4, 8, 12, …)
-m, –max-conns Maximum simultaneous connections
-h, –help Display this help screen
-d, –delay Wait between read/writes
-o, –output Dump session data to a file
-x, –hex-dump Dump session data as hex to a file
-i, –idle-timeout Idle read/write timeout
-p, –source-port port Specify source port to use
-s, –source addr Specify source address to use (doesn’t affect -l)
-l, –listen Bind and listen for incoming connections
-k, –keep-open Accept multiple connections in listen mode

-n, –nodns Do not resolve hostnames via DNS
-t, –telnet Answer Telnet negotiations
-u, –udp Use UDP instead of default TCP
–sctp Use SCTP instead of default TCP
-v, –verbose Set verbosity level (can be used several times)
-w, –wait Connect timeout
-z Zero-I/O mode, report connection status only
–append-output Append rather than clobber specified output files
–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 Specify address of host to proxy through
–proxy-type Specify proxy type (“http” or “socks4” or “socks5”)
–proxy-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

See the ncat(1) manpage for full options, descriptions and usage examples

1.临时监听TCP端口
nc -l port
2.永久监听TCP端口
nc -lk port
3.临时监听UDP
nc -lu port
4.永久监听UDP
nc -luk port

5.测试TCP端口
nc -vz ip tcp-port
6.测试UDP
nc -uvz ip udp-port

举个例子
服务器a上创建监听tcp端口(192.168.2.1)
nc -l 8080

nc -l -p 8080 > logfile.txt (监听8080端口并将数据输出到logfile.txt)
服务器b上测试端口是否畅通(192.168.2.2)
nc -vz 192.168.2.1 8080
或者
curl 192.168.2.1:8080
或者
wget 192.168.2.1:8080