我有使用logger
模拟一些“假”登录脚本:saltstack - 传递变量包含单引号,双引号,空格...到cmd.script?
#!/bin/bash
{%- set maxretry = salt['pillar.get']('fail2ban:maxretry', 3) %}
tag="$1"
message="$2"
logger -p auth.info "The {{ maxretry + 1 }} \"$tag\" below lines are generated by logger to test Fail2ban"
for i in $(seq {{ maxretry + 1 }}); do
logger -p auth.warning -t "$tag" "$message"
done
这就是所谓的宏:
fake_{{ formula }}_login:
cmd:
- script
- source: salt://fail2ban/fake_login.jinja2
- template: jinja
- args: "{{ tag|default(formula) }} '{{ message }}'"
- require:
- sls: bash
- sls: fail2ban
事情是{{消息}}可以包含单/双引号,空格,方括号,... 根据cmd.script文档,要传递一个包含空格的字符串,我们必须将其双引号。 但是,如果我有这样的事情:
{{ fail2ban_regex_test('mysql', tag='mysqld', message="150114 3:40:50 [Warning] Access denied for user 'root'@'5.6.7.8' (using password: YES)") }}
将被记录到系统日志而没有围绕用户/主机的单引号,只是:
mysqld: 150114 3:40:50 [Warning] Access denied for user [email protected] (using password: YES)
,使的fail2ban未能确认为它与过滤器正则表达式不匹配。
我可以单引号改为双引号,并使用反斜线逃避:
fake_{{ formula }}_login:
cmd:
- script
- source: salt://fail2ban/fake_login.jinja2
- template:
jinja
- args: "{{ tag|default(formula) }} \"{{ message|safe }}\""
- require:
- sls: bash
- sls: fail2ban
它处理上述情况时,消息只包含单引号。
但是,如果它包含双引号:
{{ fail2ban_regex_test('postfix', tag='postfix/smtpd[20228]', message="NOQUEUE: reject: RCPT from sender.com["5.6.7.8"]: 554 5.7.1 <[email protected]>: Recipient address rejected: Access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<mg01d1.sender.com>") }}
我得到这个错误:
local:
Data failed to compile:
----------
Rendering SLS "base:postfix.test" failed: Jinja syntax error: expected token ',', got 'float'; line 29
---
[...]
- sls: openldap
- sls: openldap.diamond
- sls: openldap.nrpe
{%- endcall %}
{{ fail2ban_regex_test('postfix', tag='postfix/smtpd[20228]', message="NOQUEUE: reject: RCPT from sender.com["5.6.7.8"]: 554 5.7.1 <[email protected]>: Recipie
nt address rejected: Access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<mg01d1.sender.com>") }} <======================
如果我试图逃跑用反斜杠双引号:
... message="NOQUEUE: reject: RCPT from sender.com[\"5.6.7.8\"] ...
那么我得到了另一个错误:
local:
Data failed to compile:
----------
Rendering SLS postfix.test failed, render error: while parsing a block mapping
in "<unicode string>", line 84, column 7:
- args: "postfix/smtpd[20228] \"NO ...
^
expected <block end>, but found '<scalar>'
in "<unicode string>", line 84, column 76:
... : reject: RCPT from sender.com["5.6.7.8"]: 554 5.7.1 <[email protected] ...
如何处理这两种情况?
如何传递一个串过'stdin'及使用'read'来把它读入一个变量。然后在双引号内使用该变量... – anishsane 2015-03-19 04:31:11
全部自动化。这里没有涉及到stdin。 – quanta 2015-03-19 04:46:38
我的理解是这样的:你有你的框架中的字符串数据,使用它,你想触发另一个脚本。您正在使用一些字符串连接逻辑来完成此操作。取而代之的是,改变'另一个脚本'以从标准输入读取数据。然后,不要使用字符串连接,而是将字符串传递给脚本。 – anishsane 2015-03-19 09:29:49