linux network tools

连通性测试

  • ping: send ICMP ECHO_REQUEST to network hosts

    # 记录路由,受限于 ip 协议的头部大小,最多可以记录9条
    ping -R x.x.x.x
    
    # 设置 icmp request 个数
    ping -c 4 x.x.x.x
    
    # 设置 ip ttl 值
    ping -t 2 x.x.x.x
    
    # 设置发送间隔(默认一秒发送一个 ICMP request)
    ping -i 0.1 x.x.x.x
    
    # 设置 packetsize 默认 56 bytes
    ping -s 2048 x.x.x.x
    
  • tracepath: traces path to a network host discovering MTU along this path

    # 不解析 IP
    tracepath -n x.x.x.x
    
    # 同时打印 IP 地址和主机名
    tracepath -b x.x.x.x
    
    # 设置最大 ttl
    tracepath -m 5 x.x.x.x
    
  • telnet: user interface to the TELNET protocol

    teletype network, 是一个面向文本的双向交互式通讯协议,发行版里面的 telnet 是 telnet 协议的实现。 telnet 可用于登录设备,创建 TCP 三次握手测试主机的 TCP 端口、发送特殊指令(如 telnet 登录设备(类似于 SSH,明文传输)、 telnet www.aa.com 80) 协议端口号默认为 23。

    #  ===== telnet web =======
    root@mercury:~# telnet 10.21.140.230 5000
    Trying 10.21.140.230...
    Connected to 10.21.140.230.
    Escape character is '^]'.
    GET /api/v1/test-page/ HTTP/1.1
    
    HTTP/1.0 200 OK
    Content-Type: application/json
    Content-Length: 19
    Server: Werkzeug/1.0.0 Python/3.6.9
    Date: Thu, 16 Jul 2020 02:32:58 GMT
    
    {
      "abc": "cde"
    }
    Connection closed by foreign host.
    
    # ======= telnet router =======
    
    telnet -l rviews  route-server.ip.att.net
    Trying 12.0.1.28...
    Connected to route-server.cbbtier3.att.net.
    Escape character is '^]'.
    -------------- route-server.ip.att.net ---------------
    ---------  AT&T IP Services Route Monitor  -----------
    rviews@route-server.ip.att.net> show route summary
    Autonomous system number: 65000
    Router ID: 12.0.1.28
    
    inet.0: 797451 destinations, 12757147 routes (797451 active, 0 holddown, 0 hidden)
                  Direct:      1 routes,      1 active
                   Local:      1 routes,      1 active
                     BGP: 12757038 routes, 797342 active
                  Static:    107 routes,    107 active
    
    inet6.0: 86531 destinations, 1384378 routes (86531 active, 0 holddown, 0 hidden)
                  Direct:      1 routes,      1 active
                   Local:      2 routes,      2 active
                     BGP: 1384372 routes,  86525 active
                  Static:      2 routes,      2 active
                   INET6:      1 routes,      1 active
    
  • nc: arbitrary TCP and UDP connections and listens

    netcat 是 linux 系统中一个很小但功能很多的网络工具。就像瑞士军刀。

    注意

    在一般的 Linux 系统中, 0-1023 这个范围的端口是要 root 才可以使用,1024 以上的端口普通用户可以使用。

    示例

    # 测试服务器 TCP 端口是否打开
    nc -v x.x.x.x 22
    nc -v x.x.x.x 22222
    
    # 端口扫描
    # TCP
    nc -vnz -w x.x.x.x 1-1024 2000-3000 2>&1 | grep succ
    # UDP
    nc -nvzu x.x.x.x 1-1024
    
    # 复制文件
    nc -l -p 1234 < test.log
    nc x.x.x.x 1234 > test.log
    
    # 发送 HTTP 请求
    # interactive
    root@mercury:~# nc 10.21.140.230 5000
    GET /api/v1/test-page/ HTTP/1.0
    
    HTTP/1.0 200 OK
    Content-Type: application/json
    Content-Length: 19
    Server: Werkzeug/1.0.0 Python/3.6.9
    Date: Thu, 16 Jul 2020 06:15:04 GMT
    
    {
      "abc": "cde"
    }
    
    # command line
    echo -ne "GET / HTTP/1.0\r\n\r\n" | nc 10.21.140.230/api/v1/test-page/
    

DNS 查询

DNS 记录类型:

  • A: 地址记录(IPv4 address)

  • AAAA: 地址记录(IPv6 address)

  • NS:域名记录服务器(Name Server),返回保存下一级域名信息的服务器域名

  • MX: 邮件记录(Mail eXchange) , 返回接收电子邮件的服务器地址

  • TXT:备注信息,有些证书机构会验证此记录来确认证书申请者对域名有控制权限

  • CNAME: 规范名称记录(Canonical Name),返回另一个域名,即当前查询的域名是另一个域名的跳转

DNS 查询工具

  • nslookup: query Internet name servers interactively

    nslookup jd.com 1.1.1.1
    nslookup -type=a jd.com 1.1.1.1
    nslookup -type=AAAA jd.com 1.1.1.1
    nslookup -type=MX jd.com 1.1.1.1
    nslookup -type=ns jd.com 1.1.1.1
    
  • dig: DNS lookup utility

HTTP 命令行工具

  • curl: transfer a URL

  • httpie: a user-friendly command-line HTTP client

  • wget: The non-interactive network downloader