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

我在文档和其他脚本中看到了一些这样的提及,但没有具体说明它们是如何使用的。 任何人都可以给我一些例子吗?

这仅仅是一个例子吗?

myvar=7 . . . [ssh] bantime=%(myvar)s 

如果是这样的话呢?

其次,如何在jail.conf中使用“动作快捷键”? 例如action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]在这?

如果您看看fail2ban中包含的规则,您会注意到它们使用这些variables来使事情变得更整齐,更具参数化。 例如,在包含的jail.conf他们使用它们来制定通用的操作规则,然后在定义各种jail时可以使用它们。

这里有一些基本的variables。

 # 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 

这些variables然后用于其他variables来构造一些基本的动作。

 # Default banning action (eg 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"] 

注意这里他们正在构造一个叫做action_通用操作,它使用其他variables,例如%(banaction)s%(port)s ,`%(protocol)s等。

man jail.conf手册页:

使用Python的“string插值”机制,其他定义是允许的,并可以在其他定义中用作%(name)。 例如。

  baduseragents = IE|wget failregex = useragent=%(baduseragents)s 

所以%(...)s是Python语言的一部分。 如果你search它们,你最终会从Python语言的规范中find这个页面,特别是这个标题为5.6.2的部分。 string格式化操作 。 这个页面上有一个例子:

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

%(...string...)s被称为Python中的string格式化或插值运算符。 %(...string...)末尾的s是一个标志,指定可能传递给它的任何Python对象都转换为string。 从我引用的链接中,有一张允许所有标志的表:

SS#1

%指定了开始说明符的位置, (...string...)是我们希望在这里展开的Pythonvariables。