我需要微调以下正则表达式。现在它给了我 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/