路由相关命令 ping, traceroute, host, dig

1. 发送ICMP包到网络主机 ping

  • ping [host]: ping特定主机[host], 展示结果包括对应ip,icmp_seq表示每次接受包的序号,ttl表示设定传输经过的最多路由数,time表示发包到接收的时间,最下面表示丢包情况。
➜  linux_commands ping baidu.com
PING baidu.com (220.181.38.148): 56 data bytes
64 bytes from 220.181.38.148: icmp_seq=0 ttl=48 time=29.664 ms
64 bytes from 220.181.38.148: icmp_seq=1 ttl=48 time=32.942 ms
64 bytes from 220.181.38.148: icmp_seq=2 ttl=48 time=37.541 ms
64 bytes from 220.181.38.148: icmp_seq=3 ttl=48 time=28.711 ms
64 bytes from 220.181.38.148: icmp_seq=4 ttl=48 time=57.416 ms
^C
--- baidu.com ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 28.711/37.255/57.416/10.543 ms
  • ping -c [num] [host]: 对主机[host] ping [num]次
➜  linux_commands ping -c 2 baidu.com
PING baidu.com (220.181.38.148): 56 data bytes
64 bytes from 220.181.38.148: icmp_seq=0 ttl=48 time=29.337 ms
64 bytes from 220.181.38.148: icmp_seq=1 ttl=48 time=28.082 ms

--- baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 28.082/28.709/29.337/0.628 ms
  • ping -i [seconds] [host]: 按[seconds](单位秒)的间隔发起ping,默认取值是1
  • ping -a [host]: ping过程中每接收一次包则响铃一次
  • ping --apple-time [host]: 当接收ping返回的包时,在最前面打印时间
➜  linux_commands ping --apple-time baidu.com
PING baidu.com (39.156.69.79): 56 data bytes
12:26:11.681634 64 bytes from 39.156.69.79: icmp_seq=0 ttl=46 time=28.437 ms
12:26:12.683875 64 bytes from 39.156.69.79: icmp_seq=1 ttl=46 time=28.177 ms
^C
--- baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 28.177/28.307/28.437/0.130 ms
  • ping -r [host]: 不经过正常路由表直接请求到主机。如果主机不是可直接连接的,则会报错
➜  linux_commands ping -r baidu.com
PING baidu.com (220.181.38.148): 56 data bytes
ping: sendto: Network is unreachable
ping: sendto: Network is unreachable
Request timeout for icmp_seq 0
ping: sendto: Network is unreachable
Request timeout for icmp_seq 1
^C
--- baidu.com ping statistics ---
3 packets transmitted, 0 packets received, 100.0% packet loss
➜  linux_commands ping -r localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.084 ms
^C
--- localhost ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.050/0.067/0.084/0.017 ms

2. 追踪路由包 traceroute

  • traceroute (-n) [host]: 打印请求包到[host]的轨迹, -n参数表示去掉ip和主机名之间的map
➜  ~ traceroute yahoo.com
traceroute: Warning: yahoo.com has multiple addresses; using 98.138.219.231
traceroute to yahoo.com (98.138.219.231), 64 hops max, 52 byte packets
 1  100.100.56.254 (100.100.56.254)  10.767 ms  9.444 ms  7.391 ms
 2  100.100.40.254 (100.100.40.254)  5.757 ms  7.284 ms  3.242 ms
 3  140.206.66.41 (140.206.66.41)  5.081 ms  6.205 ms  6.637 ms
 4  27.115.54.69 (27.115.54.69)  27.783 ms  6.741 ms  9.820 ms
 5  139.226.212.129 (139.226.212.129)  4.389 ms
    112.64.252.73 (112.64.252.73)  11.624 ms  11.605 ms
 6  139.226.210.69 (139.226.210.69)  5.939 ms

➜  ~ traceroute -n yahoo.com
traceroute: Warning: yahoo.com has multiple addresses; using 98.138.219.231
traceroute to yahoo.com (98.138.219.231), 64 hops max, 52 byte packets
 1  100.100.56.254  6.045 ms  5.008 ms  4.777 ms
 2  100.100.40.254  2.090 ms  2.420 ms  2.025 ms
 3  140.206.66.41  1.868 ms  2.140 ms  2.134 ms
 4  27.115.54.69  4.273 ms  4.295 ms  7.646 ms
 5  139.226.212.129  5.147 ms
    112.64.252.73  11.037 ms
    139.226.212.129  11.922 ms
 6  139.226.231.89  6.644 ms
  • traceroute -q [num] [host]: 指定每跳的请求数
➜  ~ traceroute -q 5 qq.com
traceroute: Warning: qq.com has multiple addresses; using 58.247.214.47
traceroute to qq.com (58.247.214.47), 64 hops max, 52 byte packets
 1  100.100.56.254 (100.100.56.254)  28.162 ms  7.337 ms  5.229 ms  7.337 ms  4.273 ms
 2  100.100.40.254 (100.100.40.254)  2.599 ms  2.481 ms  2.703 ms  1.967 ms  2.020 ms
 3  140.206.66.41 (140.206.66.41)  3.054 ms  2.262 ms  2.093 ms  2.007 ms  2.045 ms
 4  27.115.54.69 (27.115.54.69)  3.133 ms  4.080 ms  6.453 ms  4.216 ms  3.549 ms
 5  139.226.212.237 (139.226.212.237)  5.258 ms
    112.64.252.73 (112.64.252.73)  5.062 ms
    139.226.212.129 (139.226.212.129)  22.507 ms
    139.226.212.237 (139.226.212.237)  5.929 ms
    112.64.252.73 (112.64.252.73)  4.828 ms
 6  139.226.208.66 (139.226.208.66)  4.637 ms
    139.226.196.14 (139.226.196.14)  5.088 ms
    139.226.207.222 (139.226.207.222)  5.853 ms
    139.226.213.138 (139.226.213.138)  4.063 ms
    139.226.208.66 (139.226.208.66)  7.580 ms
  • traceroute [host] [num]: 指定到[host]的请求包字节数[num]
➜  ~ traceroute qq.com 60
traceroute: Warning: qq.com has multiple addresses; using 58.247.214.47
traceroute to qq.com (58.247.214.47), 64 hops max, 60 byte packets
 1  100.100.56.254 (100.100.56.254)  7.080 ms  6.848 ms  8.895 ms
 2  100.100.40.254 (100.100.40.254)  4.994 ms  5.858 ms  7.222 ms
 3  140.206.66.41 (140.206.66.41)  3.949 ms  4.770 ms  4.609 ms
 4  27.115.54.69 (27.115.54.69)  37.369 ms  5.361 ms  6.684 ms
 5  139.226.212.129 (139.226.212.129)  8.308 ms
    139.226.212.237 (139.226.212.237)  5.554 ms
    112.64.252.73 (112.64.252.73)  7.820 ms
 6  139.226.199.58 (139.226.199.58)  5.991 ms
    139.226.196.14 (139.226.196.14)  7.928 ms
    139.226.196.10 (139.226.196.10)  5.903 ms
  • trace -w [time] [host]: 每等[time]秒发起请求到[host]
➜  ~ traceroute -w 1 baidu.com
traceroute: Warning: baidu.com has multiple addresses; using 39.156.69.79
traceroute to baidu.com (39.156.69.79), 64 hops max, 52 byte packets
 1  100.100.56.254 (100.100.56.254)  8.958 ms  5.660 ms  6.188 ms
 2  100.100.40.254 (100.100.40.254)  2.644 ms  3.142 ms  2.075 ms
 3  140.206.66.41 (140.206.66.41)  2.455 ms  2.429 ms  2.607 ms
 4  27.115.54.69 (27.115.54.69)  4.242 ms  4.102 ms  3.169 ms
 5  139.226.212.129 (139.226.212.129)  5.805 ms
    139.226.212.237 (139.226.212.237)  4.865 ms  4.883 ms
 6  139.226.231.89 (139.226.231.89)  7.274 ms
    139.226.195.165 (139.226.195.165)  5.904 ms
    139.226.195.121 (139.226.195.121)  4.153 ms

3. 查找域名服务器 host

  • host (-a) [domain]: 查找域名[domain]d的A, AAAA和MX记录, -a表示查找所有记录
➜  ~ host baidu.com
baidu.com has address 39.156.69.79
baidu.com has address 220.181.38.148
baidu.com mail is handled by 15 mx.n.shifen.com.
baidu.com mail is handled by 20 jpmx.baidu.com.
baidu.com mail is handled by 20 mx1.baidu.com.
baidu.com mail is handled by 20 mx50.baidu.com.
baidu.com mail is handled by 10 mx.maillb.baidu.com.
➜  ~ host -a baidu.com
Trying "baidu.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2079
;; flags: qr rd ra; QUERY: 1, ANSWER: 15, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;baidu.com.			IN	ANY

;; ANSWER SECTION:
baidu.com.		479	IN	TXT	"v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com a mx ptr -all"
baidu.com.		479	IN	TXT	"google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM"
baidu.com.		479	IN	MX	20 mx50.baidu.com.
baidu.com.		479	IN	MX	20 mx1.baidu.com.
baidu.com.		479	IN	MX	15 mx.n.shifen.com.
baidu.com.		479	IN	MX	20 jpmx.baidu.com.
baidu.com.		479	IN	MX	10 mx.maillb.baidu.com.
baidu.com.		479	IN	SOA	dns.baidu.com. sa.baidu.com. 2012143042 300 300 2592000 7200
baidu.com.		60	IN	A	39.156.69.79
baidu.com.		60	IN	A	220.181.38.148
baidu.com.		479	IN	NS	dns.baidu.com.
baidu.com.		479	IN	NS	ns3.baidu.com.
baidu.com.		479	IN	NS	ns4.baidu.com.
baidu.com.		479	IN	NS	ns2.baidu.com.
baidu.com.		479	IN	NS	ns7.baidu.com.

Received 487 bytes from 100.100.61.99#53 in 72 ms
  • host -t [field] [domain]: 查询域名[domain]的[field]记录,field可以是CNAME, NS,A等
➜  ~ host -t A baidu.com
baidu.com has address 39.156.69.79
baidu.com has address 220.181.38.148

4. DNS查询工具 dig

  • dig +short [host] (MX): 查找域名[host]的A记录, 后面加MX表示查找MX记录
➜  ~ dig +short baidu.com
39.156.69.79
220.181.38.148
➜  ~ dig +short baidu.com MX
15 mx.n.shifen.com.
20 mx50.baidu.com.
20 mx1.baidu.com.
10 mx.maillb.baidu.com.
20 jpmx.baidu.com.
  • dig [host] ANY: 查找域名[host]的所有记录
  • dig @[dns] [host]: 指定DNS服务器[dns]查询域名[host]的记录
➜  ~ dig @8.8.8.8 baidu.com

; <<>> DiG 9.10.6 <<>> @8.8.8.8 baidu.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10752
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;baidu.com.			IN	A

;; ANSWER SECTION:
baidu.com.		447	IN	A	220.181.38.148
baidu.com.		447	IN	A	39.156.69.79

;; Query time: 46 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Jul 30 12:21:51 CST 2020
;; MSG SIZE  rcvd: 70
  • dig +nssearch [host]: 找到权威的域名服务器并展示SOA(起始授权机构)记录
➜  ~ dig +nssearch baidu.com
SOA dns.baidu.com. sa.baidu.com. 2012143047 300 300 2592000 7200 from server 112.80.248.64 in 13 ms.
SOA dns.baidu.com. sa.baidu.com. 2012143047 300 300 2592000 7200 from server 180.76.76.92 in 16 ms.
SOA dns.baidu.com. sa.baidu.com. 2012143047 300 300 2592000 7200 from server 202.108.22.220 in 27 ms.
SOA dns.baidu.com. sa.baidu.com. 2012143047 300 300 2592000 7200 from server 220.181.33.31 in 31 ms.
SOA dns.baidu.com. sa.baidu.com. 2012143047 300 300 2592000 7200 from server 14.215.178.80 in 35 ms.


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值