Python 正则表达式解析器

标签 python regex parsing alert snort

我需要微调以下正则表达式。现在它给了我 srcip、dstip、srcport、dstport 和日期。我还需要它来给我协议(protocol)(UDP、TCP)。这是它需要解析的行:

03/09-13:00:59.136048  [**] [1:2003410:9] ET POLICY FTP Login Successful [**] [Classification: Misc activity] [Priority: 3] {TCP} 172.16.112.100:21 -> 206.48.44.18:1039

这是我当前的正则表达式:

([0-9/]+)-([0-9:.]+)\s+.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})\s+->\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})

此外,它需要能够处理没有与其关联的端口的请求(例如 ICMP):

03/09-13:57:26.523602  [**] [1:2100368:7] GPL ICMP_INFO PING BSDtype [**] [Classification: Misc activity] [Priority: 3] {ICMP} 172.16.114.50 -> 172.16.112.207

最佳答案

这个正则表达式应该可以满足您的需求:

([0-9\/]+)-([0-9:.]+)\s+.*?\s\{(\w+)\}\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):?(\d{1,5})?\s+->\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):?(\d{1,5})?

我添加了 \s\{(\w+)\}\s 以匹配协议(protocol)。我还将协议(protocol)及其前面的冒号设为可选。

关于Python 正则表达式解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517224/

相关文章:

java - JSON 解析器仅获取 JSON 的一部分

python - plt.figure() 与 Matplotlib 中的子图

php - 正则表达式单词边界在 PHP 中究竟是如何工作的?

使用部分捕获进行 url 验证的正则表达式

java - 从 XML 递归打印树结构在 java 中具有奇怪的行为

java - 在 Java NetBeans 中解析 JSON 字符串

python - 如何使用python一次从文件中读取两行

python - 如何使用 img2pdf 作为模块

python - 如何将矩阵顺时针旋转 90 度?

regex - 正则表达式的重构起点