san*_*dra 0 python string shlex python-3.6
shlex.split() 没有在输入字符串上提供正确的输出。
在 python 解释器中,将输入值存储在变量中会产生预期的输出。
但是如果我通过脚本执行,shlex.split()输出不正确,输入字符串没有在空格上拆分。
>>> import shlex
>>> var = "/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep> /playbooks/ --extra-vars '{ \"text\": \"DUMMY\", \"addition\": [\"1\", \"2\", \"3\", ], \"deletion\": [], \"update\": \"update\", \"path\": \"/var/sandeep\", }' /tmp/sandeep//tmp/example.yaml"
>>>
>>>
>>> shlex.split(var)
['/usr/bin/ansible-playbook', '--timeout=60', '--module-path', '/var/sandeep/playbooks/', '--extra-vars', '{ "text": "DUMMY", "addition": ["1", "2", "3", ], "deletion": [], "update": "update", "path": "/var/sandeep", }', '/tmp/sandeep//tmp/example.yaml']
Run Code Online (Sandbox Code Playgroud)
def create_extra(text, extra_dict):
extra = "'{{ \\\"text\\\": \\\"{}\\\", ".format(text)
for key, value in extra_dict.items():
if isinstance(value, list):
extra += '\\\"{}\\\": ['.format(key)
for item in value:
extra += '\\\"{}\\\", '.format(item)
extra += '], '
elif isinstance(value, dict):
extra += '\\\"{}\\\": {{'.format(key)
for item_key, item_value in value.items():
extra += '\\\"{}\\\": \\\"{}\\\", '.format(item_key, item_value)
extra += "}, "
else:
extra += '\\\"{}\\\": \\\"{}\\\", '.format(key, value)
extra += "}'"
#print("extra: %s" % extra)
return extra
extra_dict = {'addition': ["1", "2", "3"],
'deletion': [],
'update': 'update',
'path' : '/var/sandeep'
}
temp = create_extra("DUMMY", extra_dict)
"""create_extra function formats and return string"""
cmd = ('"/usr/bin/ansible-playbook ' +
'--timeout=60 ' +
'--module-path /var/sandeep/playbooks/ ' +
'--extra-vars {} {}/{}"'.format(temp, "/tmp/sandeep", "/tmp/example.yaml"))
print(cmd)
print(shlex.split(cmd))
Run Code Online (Sandbox Code Playgroud)
output of print(cmd)
"/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep/playbooks/ --extra-vars '{ \"text\": \"DUMMY\", \"addition\": [\"1\", \"2\", \"3\", ], \"deletion\": [], \"update\": \"update\", \"path\": \"/var/sandeep\", }' /tmp/sandeep//tmp/example.yaml"
Expected results:
['/usr/bin/ansible-playbook', '--timeout=60', '--module-path', '/var/sandeep/playbooks/', '--extra-vars', '{ "text": "DUMMY", "addition": ["1", "2", "3", ], "deletion": [], "update": "update", "path": "/var/sandeep", }', '/tmp/sandeep//tmp/example.yaml']
Actual Results:
['/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep/playbooks/ --extra-vars \'{ "text": "DUMMY", "addition": ["1", "2", "3", ], "deletion": [], "update": "update", "path": "/var/sandeep", }\' /tmp/sandeep//tmp/example.yaml']
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
该shlex输出是完全正确的,因为文字的"包含在你的字符串中的字符。
cmd = ('"/usr/bin/ansible-playbook ' +
# ^- that right there
'--timeout=60 ' +
'--module-path /var/sandeep/playbooks/ ' +
'--extra-vars {} {}/{}"'.format(temp, "/tmp/sandeep", "/tmp/example.yaml"))
# and this right here -^
Run Code Online (Sandbox Code Playgroud)
正如你print(cmd)所显示的:
"/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep/playbooks/ --extra-vars whatever /tmp/sandeep//tmp/example.yaml"
Run Code Online (Sandbox Code Playgroud)
...您的字符串以 a 开头并以 a"结尾",这使得它在被 shell 解析时成为一个单一的文字字符串。
把这些字符去掉,问题就不再发生了:
cmd = ('/usr/bin/ansible-playbook ' +
'--timeout=60 ' +
'--module-path /var/sandeep/playbooks/ ' +
'--extra-vars {} {}/{}'.format(temp, "/tmp/sandeep", "/tmp/example.yaml"))
print(cmd)
print(shlex.split(cmd))
Run Code Online (Sandbox Code Playgroud)
但是,您还有其他严重的错误,因为字符串连接本质上不适合构建命令行。与其尝试采用这种方法,不如直接构建一个数组:
cmd = ['/usr/bin/ansible-playbook',
'--timeout=60',
'--module-path', '/var/sandeep/playbooks/',
'--extra-vars', temp, os.path.join('/tmp/sandeep', '/tmp/example.yml')]
Run Code Online (Sandbox Code Playgroud)
...然后temp带有空格或文字引号的或 其他变量的值将不再破坏您的代码或允许注入任意参数。
| 归档时间: |
|
| 查看次数: |
972 次 |
| 最近记录: |