首页 > 解决方案 > 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})+

我正在测试: https ://www.rsyslog.com/regex/

标签: regextemplatesrsyslog

解决方案


如果您只需要精确匹配 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"

推荐阅读