debian 使用 netcat 测试网络连通性。

安装

apt update && apt install netcat-openbsd -y
nc -h
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
          [-m minttl] [-O length] [-P proxy_username] [-p source_port]
          [-q seconds] [-s sourceaddr] [-T keyword] [-V rtable] [-W recvlimit]
          [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]]
          [destination] [port]
        Command Summary:
        -4              Use IPv4
        -6              Use IPv6
        -b              Allow broadcast
        -C              Send CRLF as line-ending
        -D              Enable the debug socket option
        -d              Detach from stdin
        -F              Pass socket fd
        -h              This help text
        -I length       TCP receive buffer length
        -i interval     Delay interval for lines sent, ports scanned
        -k              Keep inbound sockets open for multiple connects
        -l              Listen mode, for inbound connects
        -M ttl          Outgoing TTL / Hop Limit
        -m minttl       Minimum incoming TTL / Hop Limit
        -N              Shutdown the network socket after EOF on stdin
        -n              Suppress name/port resolutions
        -O length       TCP send buffer length
        -P proxyuser    Username for proxy authentication
        -p port         Specify local port for remote connects
        -q secs         quit after EOF on stdin and delay of secs
        -r              Randomize remote ports
        -S              Enable the TCP MD5 signature option
        -s sourceaddr   Local source address
        -T keyword      TOS value
        -t              Answer TELNET negotiation
        -U              Use UNIX domain socket
        -u              UDP mode
        -V rtable       Specify alternate routing table
        -v              Verbose
        -W recvlimit    Terminate after receiving a number of packets
        -w timeout      Timeout for connects and final net reads
        -X proto        Proxy protocol: "4", "5" (SOCKS) or "connect"
        -x addr[:port]  Specify proxy address and port
        -Z              DCCP mode
        -z              Zero-I/O mode [used for scanning]
        Port numbers can be individual or ranges: lo-hi [inclusive]

TCP测试

## 目标主机监听
## l监听模式,v显示详细
nc -lv -p <port> 

## 源主机发送测试
## v显示详细,z只建立连接而不进行数据传输, w连接超时时间
nc -vz -w5 <dest> <port> 

测试结果

## 目标主机
root@debian: nc -lv -p 3478
Listening on 0.0.0.0 3478
Connection received on x.x.x.x 47164

## 源主机
root@debian: nc -vz -w5 x.x.x.x 3478
Connection to x.x.x.x 3478 port [tcp/*] succeeded!

UDP测试

## 目标主机监听
## l监听模式,u模式UDP,v显示详细
nc -luv -p <port> 

## 源主机发送测试
## u模式UDP, v显示详细
echo "hello!" | nc -uv -w5 <dest> <port> 

测试结果

## 目标主机
root@debian: nc -luv -p 443
Bound on 0.0.0.0 443
Connection received on x.x.x.x 49967
XXXXXXXhello!

## 源主机
root@debian: echo "hello!" | nc -uv -w5 x.x.x.x 443
Connection to x.x.x.x 443 port [udp/https] succeeded!