tcpdump使用示例

前言

这段时间一直在研究kubernetes当中的网络, 包括通过keepalived来实现VIP的高可用时常常不得不排查一些网络方面的问题, 在这里顺道梳理一下tcpdump的使用姿势, 若有写的不好的地方, 欢迎各位道友扔砖头.

**注: **

示例环境为一套kubernetes集群, 包括k8s master node及k8s work node, 均为VM

  • 查看tcpdump可以进行抓包的网络接口
[root@10-10-40-110 ~]# tcpdump -D
1.eth0
2.docker0
3.cni0
4.vethd0fd7a3f
5.nflog (Linux netfilter log (NFLOG) interface)
6.nfqueue (Linux netfilter queue (NFQUEUE) interface)
7.eth1
8.flannel.1
9.usbmon1 (USB bus number 1)
10.vetha5e14de7
11.veth5b9890d0
12.vethf6e5a39c
13.veth59af7cc7
14.vethf98a2823
15.veth628e2234
16.veth861a08f6
17.veth0912b7b6
18.vethf2889e2b
19.vethd7109cca
20.veth421502a4
21.vethf561756e
22.any (Pseudo-device that captures on all interfaces)
23.lo [Loopback]
[root@10-10-40-110 ~]#
  • 对eth0网络接口进行抓包
[root@10-10-40-110 ~]# tcpdump -i eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
  • 对所有接口进行抓包(需要进入混杂模式, Linux kernel >= 2.2)
[root@10-10-40-110 ~]# tcpdump -i any
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
  • 以详细输出的方式进行抓包

注: 不加接口参数的话默认监听第一个网络接口, 该环境下为eth0

[root@10-10-40-110 ~]# tcpdump -v
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
  • 以更加详细输出的方式进行抓包
[root@10-10-40-110 ~]# tcpdump -vv
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
  • 以最详细输出的方式进行抓包
[root@10-10-40-110 ~]# tcpdump -vvv
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
17:03:19.298070 IP (tos 0x12,ECT(0), ttl 64, id 7354, offset 0, flags [DF], proto TCP (6), length 176)
  • 以详细输出的方式进行抓包并将数据包以十六进制和ASCII方式打印输出, 除了link level header
[root@10-10-40-110 ~]# tcpdump -v -X
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
17:04:46.063040 IP (tos 0x12,ECT(0), ttl 64, id 19261, offset 0, flags [DF], proto TCP (6), length 176)
  • 以详细输出的方式进行抓包并将数据包以十六进制和ASCII方式打印输出, 包括link level header
[root@10-10-40-110 ~]# tcpdump -v -XX
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
21:40:45.439798 IP (tos 0x12,ECT(0), ttl 64, id 34723, offset 0, flags [DF], proto TCP (6), length 176)
  • 安静模式进行抓包(输出比默认模式要少)
[root@10-10-40-110 ~]# tcpdump -q
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
  • 限定抓取的数据包个数
[root@10-10-40-110 ~]# tcpdump -c 10
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
21:49:00.612030 IP 10-10-40-110.ssh > 121.121.0.65.54289: Flags [P.], seq 2802886126:2802886314, ack 3182814556, win 1432, options [nop,nop,TS val 454928787 ecr 807548508], length 188
21:49:00.612519 IP 10-10-40-110.44078 > public1.114dns.com.domain: 11925+ PTR? 65.0.121.121.in-addr.arpa. (43)
21:49:00.623275 IP public1.114dns.com.domain > 10-10-40-110.44078: 11925 NXDomain 0/1/0 (106)
21:49:00.624629 IP 10-10-40-110.51033 > public1.114dns.com.domain: 25277+ PTR? 110.40.10.10.in-addr.arpa. (43)
21:49:00.635649 IP public1.114dns.com.domain > 10-10-40-110.51033: 25277 NXDomain* 0/1/0 (78)
21:49:00.635906 IP 10-10-40-110.39356 > public1.114dns.com.domain: 9087+ PTR? 114.114.114.114.in-addr.arpa. (46)
21:49:00.635952 IP 10-10-40-110.ssh > 121.121.0.65.54289: Flags [P.], seq 188:408, ack 1, win 1432, options [nop,nop,TS val 454928811 ecr 807548508], length 220
21:49:00.644312 IP 121.121.0.65.54289 > 10-10-40-110.ssh: Flags [.], ack 188, win 32762, options [nop,nop,TS val 807549151 ecr 454928787], length 0
21:49:00.646272 IP public1.114dns.com.domain > 10-10-40-110.39356: 9087 1/0/0 PTR public1.114dns.com. (78)
21:49:00.646443 IP 10-10-40-110.ssh > 121.121.0.65.54289: Flags [P.], seq 408:1396, ack 1, win 1432, options [nop,nop,TS val 454928821 ecr 807549151], length 988
10 packets captured
10 packets received by filter
0 packets dropped by kernel
[root@10-10-40-110 ~]#
  • 将抓取的数据保存到文件, 文件后缀为.cap

注: 若想将数据保存到文件, 同时又想查看终端输出, 可以结合tee命令和管道使用tcpdump | tee > capture.cap

[root@10-10-40-110 ~]# tcpdump -c 10 -w capture.cap
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
10 packets captured
10 packets received by filter
0 packets dropped by kernel
[root@10-10-40-110 ~]#

查看文件类型

[root@10-10-40-110 ~]# file capture.cap
capture.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)
[root@10-10-40-110 ~]#

直接通过cat查看是没法看的, 全是一堆乱码, 若想查看保存的.cap文件的内容, 可以通过tcpdump -r读取

  • 读取保存的cap文件
[root@10-10-40-110 ~]# tcpdump -r capture.cap
reading from file capture.cap, link-type EN10MB (Ethernet)
21:51:09.223140 IP 10-10-40-110.ssh > 121.121.0.65.54289: Flags [P.], seq 2802890002:2802890126, ack 3182816820, win 1432, options [nop,nop,TS val 455057398 ecr 807672709], length 124
21:51:09.596238 IP 121.121.0.65.54289 > 10-10-40-110.ssh: Flags [.], ack 124, win 32764, options [nop,nop,TS val 807673597 ecr 455057398], length 0
21:51:09.732159 IP 10.10.40.103 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 220, authtype simple, intvl 1s, length 20
21:51:10.732853 IP 10.10.40.103 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 220, authtype simple, intvl 1s, length 20
21:51:10.841674 STP 802.1s, Rapid STP, CIST Flags [Learn, Forward, Agreement], length 102
21:51:11.055641 ARP, Request who-has 10-10-40-110 tell 10.10.40.2, length 28
21:51:11.055657 ARP, Reply 10-10-40-110 is-at fa:8a:41:0f:73:00 (oui Unknown), length 28
21:51:11.733994 IP 10.10.40.103 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 220, authtype simple, intvl 1s, length 20
21:51:12.735129 IP 10.10.40.103 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 220, authtype simple, intvl 1s, length 20
21:51:12.841619 STP 802.1s, Rapid STP, CIST Flags [Learn, Forward, Agreement], length 102
[root@10-10-40-110 ~]#
  • 以最详细的方式读取保存的抓包数据
[root@10-10-40-110 ~]# tcpdump -vvv -r capture.cap
  • 以IP加端口的方式展示而不是以域名和服务名称的方式展示(有些系统需要指定-nn参数来显示端口号)
[root@10-10-40-110 ~]# tcpdump -nn
  • 抓取目标主机为10.10.40.200的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn dst host 10.10.40.200
  • 抓取源端主机为10.10.40.200的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn src host 10.10.40.200
  • 抓取源端或者目标端主机为10.10.40.200的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn host 10.10.40.200
  • 抓取所有目标网络为10.10.40.0/24的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn dst net 10.10.40.0/24
  • 抓取所有源端网络为10.10.40.0/24的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn src net 10.10.40.0/24
  • 抓取所有源端网络为10.10.40.0/24或者目标网络为10.10.40.0/24的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn net 10.10.40.0/24
  • 抓取所有目标端口为22的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn dst port 22
  • 抓取所有目标端口在1-1023范围内的所有数据包
[root@10-10-40-110 ~]# tcpdump -nn dst portrange 1-1023
  • 抓取所有目标端口范围为1-1023的TCP报文
[root@10-10-40-110 ~]# tcpdump -nn tcp dst portrange 1-1023
  • 抓取所有目标端口范围为1-1023的UDP报文
[root@10-10-40-110 ~]# tcpdump -nn udp dst portrange 1-1023
  • 抓取目标主机为10.10.40.200且目标端口为22的所有报文
[root@10-10-40-110 ~]# tcpdump -nn "dst host 10.10.40.200 and dst port 22"
  • 抓取目标主机为10.10.40.200且目标端口为22或者443的所有报文
[root@10-10-40-200 ~]# tcpdump -nn dst "host 10.10.40.200 and (dst port 22 or dst port 443)"
  • 抓取所有的ICMP报文

icmp可以替换成其他的协议, 如arp / tcp / udp / vrrp等

[root@10-10-40-110 ~]# tcpdump -nn -v icmp
  • 抓取所有的ARP或者ICMP报文
[root@10-10-40-110 ~]# tcpdump -nn -v "icmp or arp"
  • 抓取所有的广播或者多播报文
[root@10-10-40-110 ~]# tcpdump -nn "multicast or broadcast"
  • 指定抓取数据包的大小(Byte)

为0表示不限制

[root@10-10-40-110 ~]# tcpdump -nn icmp -s 100
  • 结束抓包

通常情况下按Ctl+C需要过很长时间才能够退出tcpdump, 这个时候可以采用Ctl+\的方式强制退出程序

参考

posted @ 2018-10-07 16:08  乔栋  阅读(1906)  评论(0编辑  收藏  举报