写在前面
本文看下常用网络工具。
1:ping
1.1:用途
用于检验网络的连通性。
1.2:实战
在Linux环境中执行:ping www.sina.com.cn
:
[root@localhost ~]# ping www.sina.com.cn
PING spool.grid.sinaedge.com (111.62.129.51) 56(84) bytes of data.
64 bytes from 111.62.129.51 (111.62.129.51): icmp_seq=1 ttl=49 time=18.1 ms
64 bytes from 111.62.129.51 (111.62.129.51): icmp_seq=2 ttl=49 time=25.9 ms
...
^C
--- spool.grid.sinaedge.com ping statistics ---
12 packets transmitted, 12 received, 0% packet loss, time 11018ms
rtt min/avg/max/mdev = 17.397/19.402/25.901/2.092 ms
1.3:原理以及抓包验证
1.3.1:原理
基于ICMP协议,ICMP是属于网络层的协议,基于IP协议,即在IP协议的基础上增加了相关信息来满足需求。格式如下:
1.3.2:抓包验证
-
使用tcpdump抓包
先抓着包:[root@localhost ~]# rm -f /tmp/ping_sina.pcap && tcpdump -c 5 -i any icmp and host www.sina.com.cn -w /tmp/ping_sina.pcap
tcpdump: WARNING: any: That device doesn't support promiscuous mode
(Promiscuous mode not supported on the "any" device)
tcpdump: listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes参数解释:
-c 5: 抓五个包
-i any icmp:在任意网卡抓icmp协议的包
host www.sina.com.cn:host是www.sina.com.cn
-w /tmp/ping_sina.pcap:抓到的包写到文件/tmp/ping_sina.pcap中
总的就是:对主机www.sina.com.cn抓5条任意网卡的ICMP协议的包,并将结果写到文件/tmp/ping_sina.pcap中。
成功后生成文件:
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ll | grep ping
-rw-r--r--. 1 root root 624 Feb 17 22:16 ping_sina.pcap
导出到win中并使用wires hark打开:
and
2:ifconfig
2.1:用途
该命令用来列举系统中的网络设备,即网卡列表。
2.2:分析
一般执行如下命令:
vagrant@ubuntu-xenial-01:~$ ifconfig
cni0 Link encap:Ethernet HWaddr 0a:58:0a:f4:00:01
inet addr:10.244.0.1 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::401:b4ff:fe51:bcf9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:2133 errors:0 dropped:0 overruns:0 frame:0
TX packets:2216 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:139381 (139.3 KB) TX bytes:853302 (853.3 KB)
...
解释:
cni0 Link encap:Ethernet HWaddr 0a:58:0a:f4:00:01
网卡名称 网络类型为以太网 mac地址
inet addr:10.244.0.1 Bcast:0.0.0.0 Mask:255.255.255.0
ipv4地址,广播地址,子网掩码,其中广播地址可以通过udp的方式给网络内的所有主机发消息
inet6 addr: fe80::401:b4ff:fe51:bcf9/64 Scope:Link
ipv6地址,目前还在普及中,解决ipv4地址不够用问题
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
mtu,IP数据包最大大小,metric,优先级,多个网卡到同一ip都可达时的优先级,值越小,优先级越高
3:通过netstat和lsof检查网络问题
3.1:netstat
在线上环境中有时候会遇到,明明服务是正常的,但是tcp连接却无法建立成功,到底是因为啥呢?是time_wait过多导致无可用的端口了,还是怎样?此时我们就可以使用netstat命令来看下,netstat -alepn
,如下:
[root@localhost tmp]# netstat -alepn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 20874 1022/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 21847 1224/master
tcp 0 0 192.168.10.168:22 192.168.10.94:57233 ESTABLISHED 0 27499 2209/sshd: root@not
tcp 0 0 192.168.10.168:22 192.168.10.94:60364 ESTABLISHED 0 22611 1546/sshd: root@pts
tcp 0 36 192.168.10.168:22 192.168.10.94:51775 ESTABLISHED 0 27303 2138/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN 0 20883 1022/sshd
tcp6 0 0 ::1:25 :::* LISTEN 0 21848 1224/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 0 20090 829/dhclient
udp 0 0 127.0.0.1:323 0.0.0.0:* 0 18592 678/chronyd
udp6 0 0 ::1:323 :::* 0 18593 678/chronyd
raw6 0 0 :::58 :::* 7 0 19376 702/NetworkManager
...
可以先看State列观察tcp连接的状态是否异常,比如有大量的time_wait就可能是有问题的了。
每一列是什么意思呢,比如tcp 0 0 192.168.10.168:22 192.168.10.94:57233 ESTABLISHED 0 27499 2209/sshd: root@not
,意思是:
Proto:使用的协议是tcp
Local Address:监听的是网卡192.168.10.168(其实是我vm的Linux环境)的22端口号
Foreign Address:192.168.10.94:57233,远端的服务是192.168.10.94(其实是我本地的win),端口号是57233(随机分配的端口号)
EST
ABLISHED:当前tcp连接的状态是ESTABLISHED,即正常连接状态
27499 2209/sshd:进程信息,这里是sshd,即ssh连接使用的
3.2:lsof
netstat用于列出所有的端口上的连接信息,而lsof则是用于查看某个端口号,或者是本位套接字是被哪个进程打开的,这在我们遇到类似于端口号占用的问题
,或者是确定服务是否启动成功
时非常有用,比如我们现在遇到了端口号被占用的问题,就可以执行如下命令:
[root@localhost tmp]# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1022 root 3u IPv4 20874 0t0 TCP *:ssh (LISTEN)
sshd 1022 root 4u IPv6 20883 0t0 TCP *:ssh (LISTEN)
sshd 1546 root 3u IPv4 22611 0t0 TCP localhost.localdomain:ssh->192.168.10.94:60364 (ESTABLISHED)
sshd 2138 root 3u IPv4 27303 0t0 TCP localhost.localdomain:ssh->192.168.10.94:51775 (ESTABLISHED)
sshd 2209 root 3u IPv4 27499 0t0 TCP localhost.localdomain:ssh->192.168.10.94:57233 (ESTABLISHED)
当然也可以查看本地套接字,比如查看docker的/var/run/docker.sock
:
[root@localhost tmp]# lsof /var/run/docker.sock
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dockerd-c 2851 root 5u unix 0xffff904e322fe1c0 0t0 32258 /var/run/docker.sock
4:tcpdump
just go。