正则表达式 - 匹配括号内的用户名并前置字符串
我有这样的系统日志消息:
03-25-2022 18:02:51 Local1.Notice 192.168.1.1 Mar 25 18:02:51 2022 192.168.1.1 stm[6358]: <501199> <NOTI> AP:AP01 <192.168.1.1 aa:bb:cc:dd:ee> User authenticated, mac-aa:bb:cc:dd:ee, username-my.username, IP-192.168.250.100, method-802.1x, role-blahblah
我的第一个目标是:每当该行包含“用户已验证”时,提取 username- 之后的字符串(在本例中为:my.username)并添加到前面一个字符串。
想要的结果:
MYPREPENDSTRING\my.username
第二个目标:之后我需要另一个 REGEX 来提取此行中以 IP- 开头的 IP 地址 (IP-192.168.250.100)。最终结果:
想要的结果:
192.168.250.100
这两个目标只能使用正则表达式来完成......不知道是否可能。
注意:这将在防火墙上使用,允许我们通过 Syslog 映射用户名和 IP。它使用 1 个正则表达式来匹配用户名,使用另一个正则表达式来匹配 ip
非常感谢!
I've a syslog message like this:
03-25-2022 18:02:51 Local1.Notice 192.168.1.1 Mar 25 18:02:51 2022 192.168.1.1 stm[6358]: <501199> <NOTI> AP:AP01 <192.168.1.1 aa:bb:cc:dd:ee> User authenticated, mac-aa:bb:cc:dd:ee, username-my.username, IP-192.168.250.100, method-802.1x, role-blahblah
My first goal is: everytime that the line contains "User authenticated", extract the string that comes after username- (in this case: my.username) and prepend a string.
Wanted Result:
MYPREPENDSTRING\my.username
Second goal: After that I would need another REGEX that can extract the ip address in this line that starts with IP- (IP-192.168.250.100). Final result:
Wanted result:
192.168.250.100
These two goals have to be done only with regex... don't know if it's possible.
NOTE: This is to be used on a Firewall that allows us, via Syslog, to map usernames and IP's. It uses 1 regex to match the username and another regex to match the ip
Much appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
测试
假设
用户名
可以是字母数字并包含点、破折号和下划线。还假设日志行中的
ip
始终位于username
之后。IP-(?(?:[0-9]{1,3}\.){3}[0-9]{1,3}),
将匹配 IPv4名为ip
的组中的地址(请注意,这将接受999.999.999.999
作为 有效的 IP4v 地址)用户名-(?<用户名>[a-zA- Z0-9\.-_]*),
将匹配名为username
的组中的用户名。Test
Assuming that
username
can be alphanumeric and contain dots, dashes and underscores.Also assuming that the
ip
is always after theusername
in the log lines.IP-(?<ip>(?:[0-9]{1,3}\.){3}[0-9]{1,3}),
will match the IPv4 address in a group namedip
(note that this will accept999.999.999.999
as a valid IP4v address)username-(?<username>[a-zA-Z0-9\.-_]*),
will match the username in a group namedusername
.这应该可以解决问题:
用户名位于第一个捕获组中,IP 位于第二个捕获组中。请注意,这仅适用于 IPv4 地址。
示例
说明:
编辑:
经过澄清,似乎要求提供两个单独的正则表达式。只需对现有正则表达式进行非常小的修改即可实现:
用户名:
IP 地址:
That should do the trick:
The username is in your first capturing group and the IP in the second. Note that this will only work with IPv4 addresses.
Example
Explanation:
Edit:
After clarification, it seems that it was asked for two separate regular expressions. This is possible with very minor modification of the existing regular expression:
username:
IP address:
如上所述,这将与帕洛阿尔托防火墙一起使用。
最终解决方案:
事件正则表达式:用户经过身份验证的
用户名正则表达式:(?<=用户名-)\w*[-._]?\w+
IP正则表达式:(?<=IP-)\d{1,3}.\ d{1,3}.\d{1,3}.\d{1,3}
谢谢大家!
As described, this was to be used with a Palo Alto Firewall.
Final Solution:
Event Regex: User authenticated
Username Regex: (?<=username-)\w*[-._]?\w+
IP Regex: (?<=IP-)\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}
Thanks everybody!