计算机网络 - 网络层的协议-icmp协议-arp协议-ping命令
目录
1. ICMP协议:(internet control message protocol)
1.3.7 如果ping一台服务器ping不同,可能有哪些原因
2. arp协议(address resolution protocol):
1. ICMP协议:(internet control message protocol)
互联网控制报文协议,它是TCP/IP协议簇的一个子协议,用在ip主机和路由器之间传递控制消息,控制消息是指网络通不通,主机是否可达,路由是否可用等网络本身的消息,这些控制消息虽然并不传输用户数据,但是对用户数据的传递起着重要的作用。
ping命令在封装数据的时候采用的协议
ICMP报文包含在IP数据报中,属于IP的一个用户,IP头部就在ICMP报文的前面,所以一个ICMP报文包括IP头部、ICMP头部和ICMP报文,IP头部的Protocol值为1就说明这是一个ICMP报文,ICMP头部中的类型(Type)域用于说明ICMP报文的作用及格式,此外还有一个代码(Code)域用于详细说明某种ICMP报文的类型,所有数据都在ICMP头部后面
##################################
1.1 ICMP报文格式
ICMP协议封装的头部信息占用8个字节,ICMP报文的前4个字节是统一的格式,共有三个字段:类型,代码和检验和
##################################
1.2 常用ICMP报文类型
8 Echo request——回送请求(Ping请求)
0 Echo request——回送回答(Ping请求)
3 终点不可达
##################################
1.3 ping 命令
1.3.1 -s 选项 指定包的大小
[root@localhost ~]# ping www.baidu.com -s 1000
PING www.a.shifen.com (112.80.248.76) 1000(1028) bytes of data.
1008 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=1 ttl=128 time=24.1 ms
1008 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=2 ttl=128 time=23.8 ms
1008 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=3 ttl=128 time=23.8 ms
^C
##################################
1.3.2 -c 选项 指定发包的数量
[root@localhost ~]# ping www.baidu.com -s 1000 -c 4
PING www.a.shifen.com (112.80.248.75) 1000(1028) bytes of data.
1008 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=128 time=22.2 ms
1008 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=128 time=22.8 ms
1008 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=3 ttl=128 time=22.0 ms
1008 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=4 ttl=128 time=22.1 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3008ms
rtt min/avg/max/mdev = 22.063/22.312/22.846/0.347 ms
4 packets transmitted 表示发送出去4个包
4 received 表示收到4个包
0% packet loss 丢包率为0,表示网络状况良好
如果丢包率太高,表示网络状况不是很好
time 3008ms 时间
rtt min/avg/max/mdev = 22.063/22.312/22.846/0.347 ms
##################################
1.3.3 -i 选项 指定每个包之间时间间隔
默认每个包之间时间间隔为1秒
最低可设置时间间隔为2秒
interval
[root@localhost ~]# ping wwww.baidu.com -c 3 -i 0.2
PING ps_other.a.shifen.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=32.0 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=33.5 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=128 time=32.1 ms
--- ps_other.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 404ms
rtt min/avg/max/mdev = 32.027/32.601/33.583/0.697 ms
##################################
1.3.4 -w 选项 只给你多少时间 期限
总共只给这么多时间
[root@localhost ~]# ping wwww.baidu.com -c 3 -w 2
PING ps_other.a.shifen.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=32.1 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=32.5 ms
--- ps_other.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 32.136/32.318/32.501/0.256 ms
##################################
1.3.5 -W 期限
每个ping包有多少时间,
[root@localhost ~]# ping wwww.baidu.com -c 3 -W 1
PING ps_other.a.shifen.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=32.1 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=32.6 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=128 time=32.1 ms
--- ps_other.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2012ms
rtt min/avg/max/mdev = 32.124/32.291/32.616/0.309 ms
##################################
1.3.6 -t 选项 一直发包(windows)
windows 默认发3个包
用 -t选项一直发包
C:\Users\WSF>ping www.baidu.com
正在 Ping www.a.shifen.com [112.80.248.76] 具有 32 字节的数据:
来自 112.80.248.76 的回复: 字节=32 时间=22ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=22ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=33ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=26ms TTL=55
112.80.248.76 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 22ms,最长 = 33ms,平均 = 25ms
C:\Users\WSF>ping www.baidu.com -t
正在 Ping www.a.shifen.com [112.80.248.76] 具有 32 字节的数据:
来自 112.80.248.76 的回复: 字节=32 时间=23ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=22ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=22ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=23ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=22ms TTL=55
来自 112.80.248.76 的回复: 字节=32 时间=22ms TTL=55
112.80.248.76 的 Ping 统计信息:
数据包: 已发送 = 6,已接收 = 6,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 22ms,最长 = 23ms,平均 = 22ms
Control-C
^C
用ping命令来查看网络质量
查看网络质量主要看两个值
0% packet loss 丢包率
time 2012ms 响应时间
这个响应时间是指的传输时间
##################################
1.3.7 如果ping一台服务器ping不同,可能有哪些原因
1.检查ip配置和网络物理连接
2.ping网关
3.ping 一个网址 www.baidu.com
4.ping同机房其他服务器
可能服务器宕机或者服务区挂了
5.
##################################
1.3.8 路由追踪
tracepath www.baidu.com
##################################
2. arp协议(address resolution protocol):
作用:将ip地址解析为mac地址
在局域网中,网络中实际传输的是“帧”,帧里面是有目标主机的MAC地址的。在以太网中,一个主机和另一个主机进行直接通信,必须要知道目标主机的MAC地址。但这个目标MAC地址是如何获得的呢?它就是通过地址解析协议获得的。所谓“地址解析”就是主机在发送帧前将目标IP地址转换成目标MAC地址的过程。ARP协议的基本功能就是通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的顺利进行。
.ARP请求
任何时候,当主机需要找出这个网络中的另一个主机的物理地址时,它就可以发送一个ARP请求报文,这个报文包好了发送方的MAC地址和IP地址以及接收方的IP地址。因为发送方不知道接收方的物理地址,所以这个查询分组会在网络层中进行广播。
ARP响应
局域网中的每一台主机都会接受并处理这个ARP请求报文,然后进行验证,查看接收方的IP地址是不是自己的地址,只有验证成功的主机才会返回一个ARP响应报文,这个响应报文包含接收方的IP地址和物理地址。这个报文利用收到的ARP请求报文中的请求方物理地址以单播的方式直接发送给ARP请求报文的请求方。
##################################
2.1 arp -a 查看arp缓存表
incomplete 表示没有完成
##################################
2.2 arp -d 删除某个地址
##################################
2.3 arp -s 静态绑定
静态绑定以后arp缓存表对应一行会加上PERM标识
静态绑定以后就不再会动态学习了
##################################
2.4 静态绑定有什么好处?
2.4.1 arp病毒
arp病毒会让局域网里的机器找不到正确的网关对应的mac地址,让你的arp缓存表里的网关的mac地址发生变化,使用它告诉你的新的mac地址。arp病毒利用了arp缓存表会自动更新,学习的特点。
##################################
2.4.2 防范措施:
1.安装防火墙
2.抓包工具分析出病毒
3.静态绑定正确的网关的mac地址
抓取arp包
[root@localhost lianxi]# tcpdump -i ens33 arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
21:01:37.496302 ARP, Request who-has gateway tell 192.168.44.1, length 46
21:01:37.499710 ARP, Request who-has localhost.localdomain tell gateway, length 46
21:01:37.499727 ARP, Reply localhost.localdomain is-at 00:0c:29:92:35:e8 (oui Unknown), length 28
21:01:40.426005 ARP, Request who-has gateway tell 192.168.44.1, length 46
21:01:40.997007 ARP, Request who-has gateway tell 192.168.44.1, length 46
21:01:42.006519 ARP, Request who-has gateway tell 192.168.44.1, length 46
21:01:42.502064 ARP, Request who-has localhost.localdomain (00:0c:29:92:35:e8 (oui Unknown)) tell 192.168.44.1, length 46
21:01:42.502082 ARP, Reply localhost.localdomain is-at 00:0c:29:92:35:e8 (oui Unknown), length 28
21:01:42.517117 ARP, Request who-has gateway tell localhost.localdomain, length 28
21:01:42.517226 ARP, Reply gateway is-at 00:50:56:ef:d1:5c (oui Unknown), length 46
^C
10 packets captured
11 packets received by filter
0 packets dropped by kernel
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通