歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux 網絡抓包tcpdump

Linux 網絡抓包tcpdump

日期:2017/2/28 14:46:23   编辑:Linux教程

調試網絡程序時,通常需要抓包分析。Linux下的tcpdump就很好。

Ubuntu下默認已經安裝。下面先舉個實際的例子:

比如我有一個C++程序監聽本地端口8889, 另一個newlisp程序通過TCP和其通信。

首先可以檢查一下有幾個網絡接口。

root@ www.linuxidc.com :~# tcpdump -D
1.eth0
2.any (Pseudo-device that captures on all interfaces)
3.lo

由於是本機運行,都是通過lo接口,因此後面參數用-i 3.

-i 3 代表監控的是網絡接口lo。 現在啟動tcpdump

tcpdump -i 3 tcp port 8889 -v -XX

-XX表示用16進制和ASCII文本顯示網絡包的數據。

21:19:40.151304 IP (tos 0x0, ttl 64, id 37956, offset 0, flags [DF], proto TCP (6), length 56)
localhost.8889 > localhost.55131: Flags [P.], cksum 0xfe2c (incorrect -> 0x8dc0), seq 41:45, ack 117, win 342, options [nop,nop,TS val 728981 ecr 723761], length 4
0x0000: 0000 0000 0000 0000 0000 0000 0800 4500 ..............E.
0x0010: 0038 9444 4000 4006 a879 7f00 0001 7f00 .8.D@[email protected]......
0x0020: 0001 22b9 d75b 5a53 9f4c d8c4 bded 8018 .."..[ZS.L......
0x0030: 0156 fe2c 0000 0101 080a 000b 1f95 000b .V.,............
0x0040: 0b31 0100 3355

首先顯示了時間,然後可以看到數據傳輸方向,從C++->newlisp程序。還可以看到應用層傳輸的字節數目是4.

最後可以看到0100 3355 就是發出來的4個字節。

其他很多字節應該是TCP協議自身需要的數據。不做深究。

相關閱讀:Linux下抓包工具tcpdump使用 http://www.linuxidc.com/Linux/2012-11/75080.htm

最後給一個文章,演示了很多好的用法。 http://www.linuxidc.com/Linux/2013-06/86710p2.htm

Copyright © Linux教程網 All Rights Reserved