# 使用tcpdump抓取指定ip到服务端口的数据包

2017.12.25

# 前言

有时候需要在服务器端抓包,来分析是否收到客户的请求,网络应答是什么样的。

linux服务器上可使用tcpdump进行抓包,并存储为wireshark的数据包文件,然后用wireshark打开分析。

如果需要实时查看分析结果,可以查看这篇文章 (利用Wireshark和tcpdump在windows上远程实时分析Linux上的数据包 https://blog.kyletang.work/2016/11/09/wireshark-tcpdump-linux/ (opens new window)

如果没用过wireshark,可以查看这篇文章(Wireshark网络抓包工具入门 https://blog.kyletang.work/2016/07/07/wireshark-getting-started/ (opens new window)

# 查看服务器的网卡名

已知服务器的ip为172.16.2.94,执行 ip addr 命令,得到网卡名为 eth0

img1

# 执行命令抓包

tcpdump tcp -i eth0 -t -s 0 -c 100 and port 80 and host 172.16.1.120 -w x.cap
1

eth0是网卡名

80是服务端的监听端口

172.16.1.120 是客户端的ip

x.cap 是存储数据包的文件名

-c 100 : 只抓取100个数据包,提前结束,请使用ctrl+C

img2

客户端发送请求,确认请求完成之后,在命令行ctrl+c,终止抓包

补充(通过wireshark实时分析):

D:\putty\plink.exe -ssh -pw mypassword root@172.16.2.94 /usr/sbin/tcpdump tcp -i eth0 -t -s 0 -c 100 and port 80 and host 172.16.1.120 -w - | "C:\Program Files\Wireshark\wireshark.exe" -k -i -
1

第三步:使用wireshark分析 打开x.cap文件,可以看到数据包分析结果

img3