regex - rsyslog 正则表达式不会变得贪婪
问题描述
我正在编写一个 rsyslog 模板来从事件中过滤 src 和 dst ip,但正则表达式只返回第一个匹配项。
示例事件:
ulogd[20230]: id="2002" severity="info" sys="SecureNet" sub="packetfilter" name="Packet accepted" action="accept" fwrule="89" initf="eth1" outitf="eth0 " srcmac="aa:bb:cc:dd:ee:2c" dstmac="00:11:22:ff:cc:aa" srcip="10.10.1.250" dstip="192.168.0.1" proto="6"长度="52" tos="0x00" prec="0x00" ttl="127" srcport="64405" dstport="1133" tcpflags="ACK"
模板语法
%msg:R,ERE,0,FIELD:([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})+--end%
正则表达式
([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})+
解决方案
如果您只需要精确匹配 2 个 ip,那么您可以在 2 个属性替换器中重复正则表达式模式,其中第二个指定要采用第二个匹配的 ip 地址。
使用 "..." 代表模式[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
,只是为了使其更具可读性,您将拥有
%msg:R,ERE,0,FIELD,0:...--end%
%msg:R,ERE,0,FIELD,1:...--end%
或全部:
$template outfmt,"%msg:R,ERE,0,FIELD,0:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}--end% %msg:R,ERE,0,FIELD,1:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}--end%\n"
推荐阅读
- python - 如何将样本的 x 位置与 matplotlib.pyplot 中的表格列对齐?
- json - 使用对单元格的引用进行查询
- javascript - Javascript - 处理异步竞争条件
- excel - 文件 1 中的 Excel 宏将应用于文件 2
- c++ - 创建具有此类类型属性的类 + 对其他类的继承(Node 类和 NodeAVL)
- google-apps-script - 在 Google Drive 中创建 blob 时返回 URL
- c++ - 为什么 poll() 会立即返回 fifo 上的常规文件和块?
- javascript - Vuejs:你如何将类(类)传递给模板?
- python - 当电子邮件中有图像附件时,python-table 丢失
- javascript - 按 JSON 键分组对象列表