首页/网络/抓包与流量分析速查/tcpdump 逻辑组合与括号

tcpdump 逻辑组合与括号

用 and(&&)、or(||)、not(!)组合条件;括号需要用引号包住整个表达式传给 shell,避免被 shell 解释。

用途组合多个过滤条件、取反、求并集
语法expr1 and expr2 / expr1 or expr2 / not expr / (expr)

常用示例

常用示例
tcpdump -i eth0 -nn 'host 10.0.0.9 and (port 80 or port 443)'
tcpdump -i eth0 -nn 'tcp and not port 22'
tcpdump -i eth0 -nn 'not (arp or icmp)'
括号在 shell 中有特殊含义,整个表达式一定要用单引号包起来。

易错点

  • and 优先级高于 or:a or b and c 解析为 a or (b and c),有歧义就显式加括号
  • 排除法过滤(not)对理解"少了什么包"很有效,但别把要看的目标包也排除掉

数据来源与参考

  • man 8 pcap-filter(逻辑运算符与优先级)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌