tcpdump使用之9个简单案例

       从事网络方面尤其是诸如socket原始套接字一线开发人员来说,熟悉一些网络工具的使用对分析排查问题会有很大帮助,W.Richard Stevens在其作品中推荐了几款工具,这里简要介绍其中一种:tcpdump


-----下文转自http://www.cnblogs.com/bangerlee/tag/linux/-----

9tcpdump使用实例

tcpdump能帮助我们捕捉并保存网络包,保存下来的网络包可用于分析网络负载情况,包可通过tcpdump命令解析,也可以保存成后缀为pcap的文件,使用wireshark等软件进行查看。

 

以下将给出9个使用tcpdump的例子,以说明tcpdump的具体使用方法。

 

1.针对特定网口抓包(-i选项)

当我们不加任何选项执行tcpdump时,tcpdump将抓取通过所有网口的包;使用-i选项,我们可以在某个指定的网口抓包:

linux:/tmp/lx # tcpdump -i eth0tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes10:50:28.607429 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 6495110:50:28.607436 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 116:232(116) ack 1 win 1286410:50:30.384195 arp who-has 128.128.128.35 tell 128.128.128.35

以上例子中,tcpdump抓取所有通过eth0的包。

 

2.抓取指定数目的包(-c选项)

默认情况下tcpdump将一直抓包,直到按下”ctrl+c”中止,使用-c选项我们可以指定抓包的数量:

linux:/tmp/lx # tcpdump -c 2 -i eth0tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes10:58:05.656104 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210443473:1210443589(116) ack 2583117929 win 1286410:58:05.657074 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 652112 packets captured6 packets received by filter0 packets dropped by kernel

以上例子中,只针对eth0网口抓2个包。

 

3.将抓到包写入文件中(-w选项)

使用-w选项,我们可将抓包记录到一个指定文件中,以供后续分析

linux:/tmp/lx # tcpdump -w 20120606.pcap -i eth0tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes75 packets captured150 packets received by filter0 packets dropped by kernel

应当保存为.pcap后缀的文件,方便我们使用wireshark等工具读取分析。

 

4.读取tcpdump保存文件(-r选项)

对于保存的抓包文件,我们可以使用-r选项进行读取:

linux:/tmp/lx # tcpdump -r 20120606.pcapreading from file 20120606.pcap, link-type EN10MB (Ethernet)11:01:57.392907 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210446405:1210446457(52) ack 2583119957 win 1286411:01:57.392917 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 52:168(116) ack 1 win 1286411:01:57.393649 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 52 win 65327

 

5.抓包时不进行域名解析(-n选项)

默认情况下,tcpdump抓包结果中将进行域名解析,显示的是域名地址而非ip地址,使用-n选项,可指定显示ip地址。

 

6.增加抓包时间戳(-tttt选项)

使用-tttt选项,抓包结果中将包含抓包日期:

linux:/tmp/lx # tcpdump -n -tttt -i eth02012-06-06 11:14:59.539736 IP 10.71.171.140.22 > 10.70.121.95.1787: P 1:53(52) ack 100 win 75042012-06-06 11:14:59.539754 IP 10.71.171.140.22 > 10.70.121.95.1787: P 53:105(52) ack 100 win 75042012-06-06 11:14:59.539770 IP 10.71.171.140.22 > 10.70.121.95.1787: P 105:157(52) ack 100 win 7504

 

7.指定抓包的协议类型

我们可以只抓某种协议的包,tcpdump支持指定以下协议:ip,ip6,arp,tcp,udp,wlan等。以下例子只抓取arp协议的包:

linux:/tmp/lx # tcpdump -i eth0 arptcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes11:22:26.948656 arp who-has 10.10.1.30 tell 10.10.1.2611:22:27.017406 arp who-has 10.10.1.30 tell 10.10.1.2611:22:27.078803 arp who-has 10.10.1.30 tell 10.10.1.26

 

8.指定抓包端口

如果想要对某个特定的端口抓包,可以通过以下命令:

linux:/tmp/lx # tcpdump -i eth0 port 22tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes11:41:04.387547 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 1216136825 win 6475111:41:04.387891 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1:233(232) ack 0 win 1608011:41:04.398973 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: P 0:52(52) ack 233 win 64519

 

9.抓取特定目标ip和端口的包

网络包的内容中,包含了源ip地址、端口和目标ip、端口,我们可以根据目标ip和端口过滤tcpdump抓包结果,以下命令说明了此用法:

linux:/tmp/lx # tcpdump -i eth0 dst 10.70.121.92 and port 22

 

Reference: Packet Analyzer: 15 TCPDUMP Command Examples

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值