刚从wireshark转到tcpdump,所以就用最适合wireshark思路的方式写这篇博客,以便尽快适应tcpdume,官方API:https://www.tcpdump.org/manpages/tcpdump.1.html,也可以从man里查询:http://man.he.net/?topic=tcpdump§ion=all
1 常用命令
抓取一个固定主机ip的端口号,并保存在本地
sudo tcpdump -i 接口 -w 保存的路径 host 主机IP and port 端口号 -s0 -C 包大小
sudo tcpdump -i any -w ~/Desktop/test.pcap host 127.0.0.1 and port 223 -s0 -C 100
# 抓取所有网络接口数据包,提取出包含127.0.0.1:223的数据包,并以每个并保存在 ~/Desktop/test.pcap文件下
# -C 100及每100个数据包保存为一个文件
后台抓取多个主机的数据包
nohup tcpdump -i ens3 host baidu.com or csdn.com -s0 -C 100 -w ~/test.pcap &
# 抓取 baidu.com 与 csdn.com 两个域名的地址
# 使用开头的nohup与结尾的&使tcpdump后台运行
查看后台正在执行的tcpdump:
ps -ef|grep tcpdump |grep -v 'grep'
直接kill掉进程号就可以关闭掉后台运行的tcpdump了
按抓包的时间保存数据包
tcpdump -i any -s0 -G 600 -w %Y_%m%d_%H%M_%S.pcap
# 每600秒保存一次文件
2 自定义操作
自定义网络接口
tcpdump -D # 列出所有可用的网络接口
tcpdump -i eth0 # 监听 eth0 接口
自定义端口
tcpdump -i eth1 host 192.168.0.11 and ! port 80 and ! port 25
# 抓取192.168.0.11除了80,25外的其他端口
自定义抓取数据包的个数
tcpdump -i eth1 host 192.168.0.11 -c 10000
# 抓取eth1网卡上包含192.168.0.11的数据包 10000个后退出
自定义指定协议
协议可以是:ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp and udp
sudo tcpdump -i ens33 tcp and ....(其他自定义内容)
参考链接
Linux tcpdump命令详解:https://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html
超级详细Tcpdump 的用法:https://www.jianshu.com/p/bcc24f8456a1