首页/网络/网络排障速查/逻辑组合与括号优先级
本包目录(235 条)

逻辑组合与括号优先级

复杂条件的写法规范:and/or/not 与括号,优先级 not > and > or;表达式含括号或分组时整段加单引号,必要时用转义避免 shell 干扰。

网络排障速查网络更新于 2026-10-12

组合示例

组合示例
tcpdump -nn -i eth0 'host 10.0.0.5 and (port 80 or port 443)'
tcpdump -nn -i eth0 'tcp and not port 22 and not port 443'
tcpdump -nn -i eth0 '(dst port 3306 or dst port 5432) and src net 10.0.0.0/24'
# 交互式 shell 中 ! 有历史展开风险,脚本里安全写法:
tcpdump -nn -i eth0 'not port 22'
优先级not 最高,其次 and,最后 or;不确定就加括号
引号整段表达式用单引号包裹,内部再用双引号会与 shell 冲突
常见坑bash 的 ! 触发历史展开(交互式),用 not 避开
表达式越复杂越要先用 -c 5 试跑验证语法与命中量,再放开长期抓取。

易错点

  • 括号不加引号:shell 先解析括号,tcpdump 收到残缺表达式报语法错误
  • 误信默认优先级导致条件放宽或收窄,抓到的样本与预期不符

数据来源与参考

  • pcap-filter(7):运算符与优先级
  • man 8 tcpdump

相关工具

​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌