如何在fail2ban中使用所谓的动作变量?

fpg*_*ost 9 fail2ban

我在文档和杂项脚本中看到了一些对这些的提及,但没有具体说明它们是如何使用的。谁能给我一些例子?

这只是一个案例吗

myvar=7
.
.
.
[ssh]
bantime=%(myvar)s
Run Code Online (Sandbox Code Playgroud)

如果是这样,重点是什么?

其次,如何使用 jail.conf 中的“操作快捷方式”?例如action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"],关于此的任何文档?

slm*_*slm 9

如果您查看随附的规则,fail2ban您会注意到它们使用这些变量来使事情更整洁、更参数化。例如,在包含的内容中,jail.conf他们使用它们来制定通用操作规则,然后在定义各种监狱时可以使用这些规则。

例子

以下是顶部的一些基本变量。

# Destination email address used solely for the interpolations in
# jail.{conf,local,d/*} configuration files.
destemail = root@localhost

# Sender email address used solely for some actions
sender = root@localhost

# Default protocol
protocol = tcp

# Ports to be banned
# Usually should be overridden in a particular jail
port = 0:65535
Run Code Online (Sandbox Code Playgroud)

然后在其他变量中使用这些变量来构造一些基本操作。

# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables-multiport

# The simplest action to take: ban only
action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]

# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
            %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]
Run Code Online (Sandbox Code Playgroud)

请注意,他们正在构建一个名为的通用操作,action_该操作是使用其他变量创建的,例如, %(banaction)s, %(port)s, `%(protocol)s 等。

man jail.conf手册页:

使用 Python 的“字符串插值”机制,允许其他定义,以后可以在其他定义中作为 %(name)s 使用。例如。

         baduseragents = IE|wget
         failregex = useragent=%(baduseragents)s
Run Code Online (Sandbox Code Playgroud)

所以它们%(...)s是 Python 语言的一部分。如果您搜索它们,您最终会从 Python 语言的规范中找到此页面,特别是本节标题为:5.6.2。字符串格式化操作。此页面上有一个示例:

>>> print '%(language)s has %(number)03d quote types.' % \
...       {"language": "Python", "number": 2}
Python has 002 quote types.
Run Code Online (Sandbox Code Playgroud)

%(...string...)s被称为Python中的字符串格式化或插值算。该s在的端部%(...string...)是一个标志,指定任何Python对象可以被传递给它,得到转换为字符串。从我引用的链接中,有一个包含所有允许标志的表格:

  SS#1

%指定想要的符开始,而(...string...)正是我们想要的Python变量已经在这里展开。