在 Linux 下开发应用程序,避免不了使用一些网络工具排查、分析,下面总结了一些常用的网络工具。
一、ifconfig 和 ip addr
在 windows 中经常使用 ipconfig 查询网卡、IP 和 网关等信息。在 Linux 中则一般使用 ifconfig 来查看这些信息。一般使用下面命令进行安装
yum install -y net-tools
一般使用只需要输入 ifconfig 即可:
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:50:56:0A:0B:0C
inet addr:192.168.5.24 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fe0a:b0c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:172220 errors:0 dropped:0 overruns:0 frame:0
TX packets:132379 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:87101880 (83.0 MiB) TX bytes:41576123 (39.6 MiB)
Interrupt:185 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2022 errors:0 dropped:0 overruns:0 frame:0
TX packets:2022 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2459063 (2.3 MiB) TX bytes:2459063 (2.3 MiB)
不过现在 ifconfig 已经停止维护了,现在一般使用 ip 命令
查看上述信息, 只需要输入 ip addr 即可。
ip link show #显示网络接口信息
ip link set eth0 up #开启网卡
ip link set eth0 down #关闭网卡
ip addr #显示 ip 信息
ip addr add 192.168.10.2/23 dev eth0 #设置 eth0 网卡 IP 地址 192.168.10.2
ip addr del 192.168.10.2/23 dev eth0 #删除 eth0 网卡 IP 地址
ip route show #显示系统路由
ip route list # 查看路由信息
ip route add 192.168.4.0/24 via 192.168.0.254 dev eth0 # 设置192.168.4.0网段的网关为192.168.0.254,数据走eth0接口
ip route add default via 192.168.0.254 dev eth0 # 设置默认网关为192.168.0.254
ip route del 192.168.4.0/24 # 删除192.168.4.0网段的网关
ip route del default # 删除默认路由
ip route delete 192.168.1.0/24 dev eth0 # 删除路由
二、telnet
在你的项目中经常会用到 telnet 命令, 比如: 前后端分离的项目中,前端人员想要测试后端是否正常开启, 就可以使用 telnet 查看端口是否开启, 安装命令如下:
yum install -y telnet
查看服务器是否开启侦听服务, 可以使用 telnet ip port 的方式连接:
[root@localhost ~]# telnet 192.168.4.36 80
Trying 192.168.4.36...
Connected to 192.168.4.36.
Escape character is '^]'.
输出上述信息,表明端口正常。该命令一般与 ping 命令配合使用:
比如: 查看百度的网页是否正常
[root@localhost ~]# ping www.baidu.com
PING 110.242.68.4 (110.242.68.4) 56(84) bytes of data.
64 bytes from 110.242.68.4: icmp_seq=1 ttl=128 time=11.0 ms
64 bytes from 110.242.68.4: icmp_seq=2 ttl=128 time=17.3 ms
64 bytes from 110.242.68.4vv: icmp_seq=3 ttl=128 time=16.2 ms
64 bytes from 110.242.68.4: icmp_seq=4 ttl=128 time=10.6 ms
64 bytes from 110.242.68.4: icmp_seq=5 ttl=128 time=10.2 ms
不停的打印上述信息表示主机可以访问, ctrl + c 中断, 然后使用 telnet 查看 80(http) 端口是否正常
[root@localhost ~]# telnet 110.242.68.4
Trying 110.242.68.4...
Connected to 110.242.68.4.
Escape character is '^]'.
三、netstat
netstat 一般用来查看网络连接状态的命令,以下是常用参数:
-a (all)显示所有选项,netstat 默认不显示 LISTEN 相关。
-t (tcp)仅显示 tcp 相关选项
-u (udp)仅显示 udp 相关选项
-n 不显示别名,能显示数字的全部转化成数字
-l 仅列出有在 Listen (监听) 的服務状态
-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计 (重要)
-c 每隔一个固定时间,执行该 netstat 命令。
一般在使用中加上 -anp 就够用了: netstat -anp
[root@iZ238vnojlyZ ~]# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:20000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:20002 0.0.0.0:* LISTEN
tcp 0 404 120.55.94.78:22 101.224.250.233:57844 ESTABLISHED
tcp 0 0 10.117.203.175:59520 100.100.45.131:80 ESTABLISHED
tcp 15 0 127.0.0.1:32000 127.0.0.1:31000 ESTABLISHED
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::21 :::* LISTEN
tcp6 0 0 127.0.0.1:31000 127.0.0.1:32000 ESTABLISHED
四、traceroute
traceroute是用来检测发出数据包的主机到目标主机之间所经过的网关数量的工具,一般不常用,这里就不过多介绍了。
五、nc
nc 全名 netcat, 被誉为网络工具的 “瑞士军刀", 安装方法:
yum install -y nc
使用 nc 可以用来模拟客户端和服务器,不管你是开发哪一个方面,使用它都可以用来模拟另一端:-l 是 listen 监听的意思 -v 是显示更详细的信息。
模拟服务器:
[root@localhost wx]# nc -v -l 127.0.0.1 8888
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Linsening on 127.0.0.1:8888
模拟客户端:
nc -v 127.0.0.1 8888
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:8888.
双发互发消息,即可看到对端屏幕显示。
六、curl
curl 命令是发送 http 请求的命令,在写接口的过程中可以用来测试接口, 相当于 PostMan。 安装方法如下:
yum install -y curl
http 请求中 GET 和 POST 最常用, 使用 -x 来指定 GET 或者 POST
curl -x GET http://www.taobao.com
POST 请求一般都会携带数据, 使用 -d 参数来添加数据
curl -x POST -d 'somepostdata' 'http://www.youwebsite.com/api/v1/data'
我们有时候需要在发送 http 请求时设置增加一些头部信息,可以使用 -H(或 –header)指定,如果有多个选项可以使用多次 -H 来逐一设置。例如:
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: application/json' -H 'X-Requested-With: XMLHttpRequest' -d 'somepostdata' 'https://www.somesite.com/api/v1/chat'
七、lsof
lsof 意思是 list opened filedesciptor (列出已经打开的文件描述符),Linux 中一切接文件,所有东西都可以抽象问 文件描述符。
安装方法如下:
yum install -y lsof
lsof 有很多参数,但是在实际应用中用的最多的是 -Pni, 即 lsof -i 默认也会显示 ip 地址和端口号的别名,我们只要使用 -n 和 -P 选项就能相对应地显示 ip 地址和端口号了。加起来就是 -Pni.
-p<进程号> 列出指定进程号所打开的文件
-n<目录> 列出使用NFS的文件
-i<条件> 列出符合条件的进程。(4、6、协议、:端口、 @ip )
[root@bogon /]# lsof -Pni
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 714 rpc 6u IPv4 19506 0t0 UDP *:111
rpcbind 714 rpc 7u IPv4 19611 0t0 UDP *:876
rpcbind 714 rpc 8u IPv4 19612 0t0 TCP *:111 (LISTEN)
rpcbind 714 rpc 9u IPv6 19613 0t0 UDP *:111
rpcbind 714 rpc 10u IPv6 19614 0t0 UDP *:876
rpcbind 714 rpc 11u IPv6 19615 0t0 TCP *:111 (LISTEN)
avahi-dae 761 avahi 12u IPv4 22548 0t0 UDP *:5353
avahi-dae 761 avahi 13u IPv4 22549 0t0 UDP *:58067
chronyd 786 chrony 5u IPv4 22522 0t0 UDP 127.0.0.1:323
chronyd 786 chrony 6u IPv6 22523 0t0 UDP [::1]:323
cupsd 1367 root 10u IPv6 27643 0t0 TCP [::1]:631 (LISTEN)
cupsd 1367 root 11u IPv4 27644 0t0 TCP 127.0.0.1:631 (LISTEN)
sshd 1369 root 3u IPv6 27493 0t0 TCP *:22 (LISTEN)
sshd 1369 root 4u IPv4 27600 0t0 TCP *:22 (LISTEN)
dnsmasq 1563 nobody 3u IPv4 29067 0t0 UDP *:67
master 1652 root 13u IPv4 29410 0t0 TCP 127.0.0.1:25 (LISTEN)
master 1652 root 14u IPv6 29411 0t0 TCP [::1]:25 (LISTEN)
sshd 59405 root 3u IPv4 180911 0t0 TCP 192.168.207.222:22->192.168.207.1:44198 (ESTABLISHED)
八、tcpdump
windows 中常用的抓包工具是 wireshark, Linux 中则使用 tcpdump 来抓取数据包。安装发法如下:
yum install -y tcpdump
tcpdump 常用的选项有:
-
-i 指定要捕获的目标网卡名,网卡名可以使用前面章节中介绍的 ifconfig 命令获得;如果要抓所有网卡的上的包,可以使用 any 关键字。
## 抓取网卡ens33上的包 tcpdump -i ens33 ## 抓取所有网卡上的包 tcpdump -i any
-
-X 以 ASCII 和十六进制的形式输出捕获的数据包内容,减去链路层的包头信息;-XX 以 ASCII 和十六进制的形式输出捕获的数据包内容,包括链路层的包头信息。
-
-n 不要将 ip 地址显示成别名的形式;-nn 不要将 ip 地址和端口以别名的形式显示。
-
-S 以绝对值显示包的 ISN 号(包序列号),默认以上一包的偏移量显示。
-
-vv 抓包的信息详细地显示;-vvv 抓包的信息更详细地显示。
-
-w 将抓取的包的原始信息(不解析,也不输出)写入文件中,后跟文件名:
tcpdump -i any -w filename
-
-r 从利用 -w 选项保存的包文件中读取数据包信息。
如下实例:
假设我的服务器端的地址是 127.0.0.0.1:12345,使用 nc 命令在一个 shell 窗口创建一个服务器程序并在这个地址上进行侦听。
[root@bogon /]# nc -v -l 127.0.0.1 8888
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Listening on 127.0.0.1:8888
在另外一个 shell 窗口开启 tcpdump 抓包:
[root@localhost wz]# tcpdump -i any 'port 8888' -XX -nn -vv
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
然后再开一个 shell 窗口,利用 nc 命令创建一个客户端去连接服务器:
[root@localhost wz]# nc -v 127.0.0.1 8888
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:8888.
抓取的包如下:
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
03:37:03.074125 IP (tos 0x0, ttl 64, id 30797, offset 0, flags [DF], proto TCP (6), length 60)
127.0.0.1.55160 > 127.0.0.1.8888: Flags [S], cksum 0xfe30 (incorrect -> 0x2095), seq 3966708897, win 43690, options [mss 65495,sackOK,TS val 476861676 ecr 0,nop,wscale 7], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 003c 784d 4000 4006 c46c 7f00 0001 E..<xM@.@..l....
0x0020: 7f00 0001 d778 22b8 ec6f 2ca1 0000 0000 .....x"..o,.....
0x0030: a002 aaaa fe30 0000 0204 ffd7 0402 080a .....0..........
0x0040: 1c6c 54ec 0000 0000 0103 0307 0000 0000 .lT.............
0x0050: 0000 0000 0000 0000 0000 0000 ............
03:37:03.074159 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60)
127.0.0.1.8888 > 127.0.0.1.55160: Flags [S.], cksum 0xfe30 (incorrect -> 0x1d92), seq 2469199468, ack 3966708898, win 43690, options [mss 65495,sackOK,TS val 476861676 ecr 476861676,nop,wscale 7], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 003c 0000 4000 4006 3cba 7f00 0001 E..<..@.@.<.....
0x0020: 7f00 0001 22b8 d778 932c fe6c ec6f 2ca2 ...."..x.,.l.o,.
0x0030: a012 aaaa fe30 0000 0204 ffd7 0402 080a .....0..........
0x0040: 1c6c 54ec 1c6c 54ec 0103 0307 0000 0000 .lT..lT.........
0x0050: 0000 0000 0000 0000 0000 0000 ............
03:37:03.074208 IP (tos 0x0, ttl 64, id 30798, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.55160 > 127.0.0.1.8888: Flags [.], cksum 0xfe28 (incorrect -> 0xefd6), seq 1, ack 1, win 342, options [nop,nop,TS val 476861676 ecr 476861676], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 0034 784e 4000 4006 c473 7f00 0001 E..4xN@.@..s....
0x0020: 7f00 0001 d778 22b8 ec6f 2ca2 932c fe6d .....x"..o,..,.m
0x0030: 8010 0156 fe28 0000 0101 080a 1c6c 54ec ...V.(.......lT.
0x0040: 1c6c 54ec 0000 0000 0000 0000 0000 0000 .lT.............
0x0050: 0000 0000 ....
03:37:34.625477 IP (tos 0x0, ttl 64, id 30799, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.55160 > 127.0.0.1.8888: Flags [F.], cksum 0xfe28 (incorrect -> 0x7496), seq 1, ack 1, win 342, options [nop,nop,TS val 476893227 ecr 476861676], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 0034 784f 4000 4006 c472 7f00 0001 E..4xO@.@..r....
0x0020: 7f00 0001 d778 22b8 ec6f 2ca2 932c fe6d .....x"..o,..,.m
0x0030: 8011 0156 fe28 0000 0101 080a 1c6c d02b ...V.(.......l.+
0x0040: 1c6c 54ec 0000 0000 0000 0000 0000 0000 .lT.............
0x0050: 0000 0000 ....
03:37:34.625516 IP (tos 0x0, ttl 64, id 43649, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.8888 > 127.0.0.1.55160: Flags [.], cksum 0xfe28 (incorrect -> 0xf955), seq 1, ack 2, win 342, options [nop,nop,TS val 476893228 ecr 476893227], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 0034 aa81 4000 4006 9240 7f00 0001 E..4..@.@..@....
0x0020: 7f00 0001 22b8 d778 932c fe6d ec6f 2ca3 ...."..x.,.m.o,.
0x0030: 8010 0156 fe28 0000 0101 080a 1c6c d02c ...V.(.......l.,
0x0040: 1c6c d02b 0000 0000 0000 0000 0000 0000 .l.+............
0x0050: 0000 0000 ....
03:37:34.625752 IP (tos 0x0, ttl 64, id 43650, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.8888 > 127.0.0.1.55160: Flags [F.], cksum 0xfe28 (incorrect -> 0xf954), seq 1, ack 2, win 342, options [nop,nop,TS val 476893228 ecr 476893227], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 0034 aa82 4000 4006 923f 7f00 0001 E..4..@.@..?....
0x0020: 7f00 0001 22b8 d778 932c fe6d ec6f 2ca3 ...."..x.,.m.o,.
0x0030: 8011 0156 fe28 0000 0101 080a 1c6c d02c ...V.(.......l.,
0x0040: 1c6c d02b 0000 0000 0000 0000 0000 0000 .l.+............
0x0050: 0000 0000 ....
03:37:34.625770 IP (tos 0x0, ttl 64, id 50971, offset 0, flags [DF], proto TCP (6), length 52)
127.0.0.1.55160 > 127.0.0.1.8888: Flags [.], cksum 0xf953 (correct), seq 2, ack 2, win 342, options [nop,nop,TS val 476893228 ecr 476893228], length 0
0x0000: 0000 0304 0006 0000 0000 0000 0000 0800 ................
0x0010: 4500 0034 c71b 4000 4006 75a6 7f00 0001 E..4..@.@.u.....
0x0020: 7f00 0001 d778 22b8 ec6f 2ca3 932c fe6e .....x"..o,..,.n
0x0030: 8010 0156 f953 0000 0101 080a 1c6c d02c ...V.S.......l.,
0x0040: 1c6c d02c 0000 0000 0000 0000 0000 0000 .l.,............
0x0050: 0000 0000