How to use tcpdump with examples

本文详细介绍了如何使用tcpdump进行网络包抓取及分析。涵盖了从指定接口捕获数据包、限制捕获数量、查看ASCII及HEX格式数据包、保存与读取数据包文件、指定IP地址及时间戳过滤等高级用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tcpdump

1.What is tcpdump?

    Tcpdump prints out a description ofthe contents of packets on a network interface that match the boolean expression.It can also be run with the -w(write) flag,which causes it to save the packet data to a file for later analysis,and/or with the -r(read) flag,which causes it to read from a saved packet file rather than to read packets from a network interface.In all cases,only packets that match the book expression will be processed by tcpdump.

    Tcpdump will,if not run with the -c(count) flag,continue capturing packets until it is interrupted by a SIGINIT signal(generated,for example,by typing your interrupt character,typically control-C) or a SIGTERM signal(typically generated with the kill command);if run with the -c flag,it will capture packets until it is interrupted by a SIGINT or SIGTERM signal or the specified number of packets have been processed.

When tcpdump finishes  capturing packets,it will report counts of:

  • packets 'captured'(this is the number of packets that tcpdump has received and processed);
  • packets 'receieved by filter'(the meaning of this depends on the OS on which you're running tcpdump,and possibly on the way the OS was configured - if a filter was specified on the command line,on some OSes it counts packets regardless of whether they matched by the filter expression,regradless of whether tcpdump has read and processed them yet,on toher 0Ses it counts only packets that were matched by the filter expression regardless of whether tcpdump has read and processed them yet,and on other OSes it counts only packets that were matched by the filter expression and were processed by tcpdump);
  • packets 'dropped by kernel'(this is the number of packets that were dropped,due to a lack of buffer space,by the packet capture mechanism in the OS on which tcpdump is running,if the OS reports that information to applications;if not,it will be reported as 0).


Reading packets from a network interface may require that you have special privileges.Reading a saved packet file doesn't require special privileges.

2.Capture packets from a particular network interface using -i option

    When you execute tcpdump command without any option,it will capture all the packets flowing throught all the interfaces.-i option with tcpdump command,allows you to filter on a particular network interface.

   

    In this example,tcpdump captured all the packets flows in the interface lo and displays in the standard output.

3.Capture only N number of packets using -c option

    When you execute tcpdump command it gives packets until you cancel the tcpdump command.Using -c option you can specify the number of packets to capture.

    The above tcpdump command captured only 2 packets from interface lo.

4.Display captured packets in ASCII using -A option

    The following tcpdump syntax prints the packet in ASCII.


5.Display captured packets in HEX and ASCII using -XX option

    Some users might want to analyse the packets in hex values.Tcpdump provides a way to print packets in both ASCII and HEX format.

6.Capture the packets and write into a file using -w option

    Tcpdump allows you to save the packets to a file,and later you can use the packet file for further analysis.

    -w option writes the packets into a given file.The file extension should be .pcap,which can be read by any network protocol analyzer.

7.Reading the packets from a saved file using -r option

    You can read the captured pcap file and view the packets for anaysis,as shown below.

8.Capture packets with IP address using -n option

    In all the above examples,it prints packets with the DNS address,but not the ip address.The following example captures the packets and it will display the IP address of the machines involved.


9.Capture packets with proper readable timestamp using -tttt option


10.Read packets longer than N bytes

You can receive only the packets greater than n number of bytes using a filter 'greater' throught tcpdump command

tcpdump -i lo greater 1024





转载于:https://my.oschina.net/huihu/blog/186935

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值