正则表达式难题 求指点 谢谢

2019-03-21 11:32:23 +08:00
 notgood
求 V 友帮忙看看指点一下 谢谢!
目的是想用 fail2ban 屏蔽来探测的 ip
log 如下 :

Aug 15 08:59:07 <hostname> ss-server[1382]: 2018-08-15 08:59:07 ERROR: failed to handshake with <HOST>: authentication error

正规表达式如下:
failregex = ^\w+\s+\d+ \d+:\d+:\d+\s+%(__prefix_line)sERROR:\s+failed to handshake with <HOST>: authentication error$

我测试却没法匹配, 不知道问题出在哪?

fail2ban-regex '2019-03-21 02:50:41 ERROR: failed to handshake with 95.179.169.185: authentication error' '^\w+\s+\d+ \d+:\d+:\d+\s+%(__prefix_line)sERROR:\s+failed to handshake with <HOST>: authentication error$'

Running tests
=============

Use failregex line : ^\w+\s+\d+ \d+:\d+:\d+\s+%(__prefix_line)sERROR:\s...
Use single line : 2019-03-21 02:50:41 ERROR: failed to handshake wit...


Results
=======

Failregex: 0 total

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
| [1] Year(?P<_sep>[-/.])Month(?P=_sep)Day 24hour:Minute:Second(?:,Microseconds)?
`-

Lines: 1 lines, 0 ignored, 0 matched, 1 missed
[processed in 0.00 sec]

|- Missed line(s):
| 2019-03-21 02:50:41 ERROR: failed to handshake with 95.179.169.185: authentication error
`-
926 次点击
所在节点    问与答
2 条回复
jcsmallming
2019-03-22 10:01:35 +08:00
第二个语句把 '%(__prefix_line)s' 换成 '.*' 倒是能匹配,不太清楚你的 '%(__prefix_line)s' 会替换成什么内容。
第一个语句看起来应该就是不能匹配的,毕竟你用的是 '+' ,你用 '2019-03-21...' 去匹配,开头的 '^\w+' 要求的至少出现一个字符就已经不能满足了。
jcsmallming
2019-03-22 10:07:44 +08:00
@jcsmallming 啊,记错了,\w 也能匹配数字来着,第一个语句的问题应该是没有正确匹配到日期的 '-' 符号,不好意思

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/546930

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX