centos 端口测试之nc使用

服务器端口测试是否正常,运维一般使用telnet来检查,但它有局限性,服务器的端口必须存在服务运行。
这时使用nc可以在服务端模拟开启一个端口,再通过nc测试此端口,好用!
nc是netcat工具的简称,一个网络工具,可以用来端口扫描、文件传输等功能。
centos上面安装也很简单:

yum install nc -y

nc常用功能

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

一般nc只用来做TCP/UDP协议的端口测试,其它功能少用!

nc帮助说明 # 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 <command> Executes the given command via /bin/sh -e, --exec <command> Executes the given command --lua-exec <filename> Executes the given Lua script -g hop1[,hop2,...] Loose source routing hop points (8 max) -G <n> Loose source routing hop pointer (4, 8, 12, ...) -m, --max-conns <n> Maximum <n> simultaneous connections -h, --help Display this help screen -d, --delay <time> Wait between read/writes -o, --output <filename> Dump session data to a file -x, --hex-dump <filename> Dump session data as hex to a file -i, --idle-timeout <time> 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 <time> 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 <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常用案例 TCP监听测试

nc可作为server端192.168.10.11启动一个tcp的监听

nc -l 80

客户端测试方法:直接telnet该机器ip+端口

telnet 192.168.10.11 80
或:nmap 192.168.10.11 -p 80

UDP监听测试

nc作为server端启动一个udp的监听

nc -lu 80

通过netstat可以看到udp协议已经监听:

> # netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 0.0.0.0:80 0.0.0.0:* 15401/nc

客户端测试方法:(telnet只能测试tcp协议)

nc -vuz 192.168.10.11 80
(u表示udp端口,v表示可视化输出,z表示扫描时不发送数据)

使用nc传输文件

使用nc传输文件不用scp或rsync那种输入密码的操作,当然也存在一定安全性。

例如:要把A机器上的一个sql文件发送到B机器上
(需要先开启侦听B机器端口,再到A机器发送数据到该端口)

步骤1,先在B机器上启动一个接收文件的监听,格式如下

nc -l 888 > zabbix.sql

步骤2,在A机器上往B机器的888端口发送数据,把下面sql包发送过去

nc 192.168.10.11 888 < zabbix.sql

也可通过nc传输目录,但这里不推荐使用,暂不举例。

网络测试

这里也不推荐使用nc,直接使用speedtest,简单:

wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py chmod +x speedtest-cli ./speedtest-cli 解决telnet 无法断开问题

用CTRL+]键,这时会强制退到telnet命令界面下,再输入quit退出就行。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyspjg.html