linux系统之网络篇
ping命令
参数 | 作用 |
---|---|
-c | 指定ping的次数 |
-q | 静默模式,只显示ping的结果,一般和-c一起使用 |
-s | 指定ping的数据包的大小,ping默认是64字节,例如 -s 3000(字节数) |
-t | 指定ping的TTL,TTL即生存时间,就是数据包被路由器丢弃之前允许通过的路由器跳数 |
-i | 指定ping的间隔时间 |
-f | -f即flood ping,我们可以将其称为疯狂模式,就是尽全力发送数据包,但是这个模式只有root用户才可以使用 |
下面就为大家演示一下上面参数的用法
指定ping的次数
// 如大家所见,linux系统与windows在ping使用上的不同就在于,windows默认在ping完4次之后就自动结束,而linux在不手动停止的情况下会一直ping下去。那有没有方法那解决呢?当然有我们可以使用-c选项来指定ping的次数
C:\Users\admin>ping www.baidu.com
正在 Ping www.baidu.com [36.152.44.96] 具有 32 字节的数据:
来自 36.152.44.96 的回复: 字节=32 时间=21ms TTL=55
来自 36.152.44.96 的回复: 字节=32 时间=21ms TTL=55
来自 36.152.44.96 的回复: 字节=32 时间=21ms TTL=55
来自 36.152.44.96 的回复: 字节=32 时间=21ms TTL=55
36.152.44.96 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 21ms,最长 = 21ms,平均 = 21ms
[root@192 ~]# ping www.baidu.com
PING www.baidu.com (36.152.44.96) 56(84) bytes of data.
64 bytes from localhost (36.152.44.96): icmp_seq=1 ttl=128 time=22.3 ms
64 bytes from localhost (36.152.44.96): icmp_seq=2 ttl=128 time=21.10 ms
64 bytes from localhost (36.152.44.96): icmp_seq=3 ttl=128 time=21.5 ms
64 bytes from localhost (36.152.44.96): icmp_seq=6 ttl=128 time=21.5 ms
64 bytes from localhost (36.152.44.96): icmp_seq=7 ttl=128 time=22.4 ms
64 bytes from localhost (36.152.44.96): icmp_seq=8 ttl=128 time=22.1 ms
64 bytes from localhost (36.152.44.96): icmp_seq=9 ttl=128 time=22.2 ms
64 bytes from localhost (36.152.44.96): icmp_seq=10 ttl=128 time=21.9 ms
64 bytes from localhost (36.152.44.96): icmp_seq=11 ttl=128 time=22.1 ms
64 bytes from localhost (36.152.44.96): icmp_seq=12 ttl=128 time=22.4 ms
^C
--- www.baidu.com ping statistics ---
12 packets transmitted, 10 received, 16.6667% packet loss, time 150ms
rtt min/avg/max/mdev = 21.509/22.050/22.424/0.337 ms
[root@192 ~]# ping -c 2 www.baidu.com
PING www.baidu.com (36.152.44.96) 56(84) bytes of data.
64 bytes from localhost (36.152.44.96): icmp_seq=1 ttl=128 time=23.0 ms
64 bytes from localhost (36.152.44.96): icmp_seq=2 ttl=128 time=23.4 ms
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 11ms
rtt min/avg/max/mdev = 23.044/23.240/23.436/0.196 ms
只看结果
// 有人觉得ping显示的东西太多、太复杂只想查看结果,可以吗?也可以我们可以使用-q选项来实现
[root@192 ~]# ping -c2 -q www.baidu.com
PING www.baidu.com (36.152.44.96) 56(84) bytes of data.
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 22.748/22.878/23.009/0.199 ms
不知道大家有没有注意到这个mdev呢
- 这个mdev是 Mean Deviation的缩写,翻译为平均偏差。
- 表示的是ICMP包的RTT偏离平均值的程度,是用来衡量网速的稳定性的。mdev的值越大说明网速越不稳定。
- 在不同的操作系统中名字也不一样,在mac上叫stddev,在windows上则没有这个指标
下面就给你们一个在网络实际的使用中的RTT的参考值
场景 | RTT参考值 |
---|---|
ping 本机 | 0.01ms |
ping 同机房的机器 | 0.1ms |
ping 同城的机器 | 1ms |
ping 不同城市的机器 | 20ms |
南方(北)ping北方(南)机器 | 50s |
国内ping国外机器 | 200ms |
以上数据仅供参考,一切以实际的数据为主
指定ping的数据包的大小
// 在默认情况下ping命令是以64字节大小数据包来测试网络联通性的,如需改默认数据包大小可以使用-s参数
[root@192 ~]# ping -c2 -s 2048 www.baidu.com
PING www.a.shifen.com (36.152.44.95) 2048(2076) bytes of data.
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 15ms
指定ping的TTL
// TTL是由发送主机来设置的,为了防止数据包在网络中无限循环,每个路由器在转发网络数据包时,都要求将TTL的值减少1,直到TTL减少到0为止,我们可以使用-t来指定数据包在网络上的存活时间
[root@192 ~]# ping -c2 -t 120 baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=31.5 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=31.9 ms
--- baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 31.494/31.693/31.892/0.199 ms
如果不使用-t来设置TTL,那么ping会采用默认的TTL值
下面是常用系统的默认TTL值
- Linux的TTL值是64或255
- windows NT/2000/XP系统的TTL值为128
- UNIX的TTL值默认是255
指定ping的间隔时间
// ping 默认间隔是1秒,root用户可以设置间隔0.2秒但其他用户不行。
[root@192 ~]# ping -c3 -i2 baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=35.1 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=27.9 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=29.2 ms
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 12ms
rtt min/avg/max/mdev = 27.892/30.725/35.055/3.116 ms
疯狂模式ping
// -f在每秒发送100个左右的数据包
[root@192 ~]# ping -f -c 80 baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
........
--- baidu.com ping statistics ---
80 packets transmitted, 72 received, 10% packet loss, time 297ms
rtt min/avg/max/mdev = 27.547/44.247/136.466/27.111 ms, pipe 10, ipg/ewma 16.403/41.650 ms
// 10% packet loss这个表示丢包率
nc网猫
server的IP是192.168.182.142
test的IP为192.168.182.143
// nc命令,全称为net cat,中文名叫网猫
[root@server ~]# yum -y install nc // 没有nc命令的可以安装
[root@server ~]# nc -l 666 //使用-l选择监听666端口号
[root@test ~]# nc 192.168.182.142 666 // 在另一台主机上通过对端的IP地址加监听的端口号进行连接
[root@test ~]# nc 192.168.182.142 666 // 这样就可以通过本机给对端发消息了
我是143主机
[root@server ~]# nc -l 666
我是143主机
端口扫描
// 扫描本地的20到23端口号,发现只有22端口号能够连接成功,或者说只有22端口号处于开放状态
[root@server ~]# nc -z -v -n -w 2 127.0.0.1 20-23
nc: connect to 127.0.0.1 port 20 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 21 (tcp) failed: Connection refused
Connection to 127.0.0.1 22 port [tcp/*] succeeded!
nc: connect to 127.0.0.1 port 23 (tcp) failed: Connection refused
-z 选项:一旦建立连接后马上断开,而不发送和接收任何数据。
-v 选项:打印详细输出信息
-n 选项:直接使用IP地址,而不使用域名服务器来查询其域名
-w 选项:设置连接的超时时间,单位为秒
-u 选项:使用UDP建立连接。上面的命令无此设置,则表示使用TCP建立连接。
// 当连接到服务器上的某个端口号,监听该端口号的服务会传送一个Banner信息,我们使用22端口号来进行尝试
[root@server ~]# nc -v 127.0.0.1 22
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
SSH-2.0-OpenSSH_8.0 //此处为版本信息
传输文件
// 说起两台服务器之前进行传输文件,我们第一反应是使用scp或者ftp,但是nc也可以用来文件传输
[root@server ~]# ls
anaconda-ks.cfg test
[root@server ~]# nc -v -l 123 < test
Listening on 0.0.0.0 123
[root@test ~]# ls
anaconda-ks.cfg
[root@test ~]# nc -v -n 192.168.182.142 123 > test
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection refused.
[root@server ~]# nc -v -l 123 < test
Listening on 0.0.0.0 123
Connection received on 192.168.182.143 40338