ValueError:无结束引号-slappasswd包装器

时间:2018-06-22 16:53:00

标签: python getpass

我正在使用getpass简单有效地编辑LDAP密码。当我使用密码时,例如:c;_pr8\\E0L)ec*\'E._rPBH?"4i9Tne并尝试运行该程序时,出现以下错误:

Traceback (most recent call last):
  File "/usr/local/bin/editpasswd", line 73, in <module>
    main()
  File "/usr/local/bin/editpasswd", line 66, in main
    users_generated_password = generate_password()
  File "/usr/local/bin/editpasswd", line 52, in generate_password
    return send_command(PASSWORD_CREATION_COMMAND.format(users_password=password_to_generate))
  File "/usr/local/bin/editpasswd", line 33, in send_command
    command = shlex.split(command)
  File "/usr/lib/python2.7/shlex.py", line 279, in split
    return list(lex)
  File "/usr/lib/python2.7/shlex.py", line 269, in next
    token = self.get_token()
  File "/usr/lib/python2.7/shlex.py", line 96, in get_token
    raw = self.read_token()
  File "/usr/lib/python2.7/shlex.py", line 172, in read_token
    raise ValueError, "No closing quotation"
ValueError: No closing quotation

我如何调用密码:

PASSWORD_CREATION_COMMAND = "slappasswd -s {users_password}"

def send_command(command):
    command = shlex.split(command)
    sender = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
    )
    res, err = sender.communicate()
    if res:
        return res.strip()
    if err:
        return err.strip()

def generate_password():
    password_to_generate = getpass.getpass(
        prompt="Enter the new password: "
    )
    confirm_password_to_generate = getpass.getpass(
        prompt="Confirm the password you just entered: "
    )
    if password_to_generate == confirm_password_to_generate:
        return send_command(PASSWORD_CREATION_COMMAND.format(users_password=password_to_generate))
    else:
        print("[!] password did not match")
        exit(-1)

我认为这是导致该命令运行的原因,密码中同时包含"',所以密码失败,因为它试图查找结尾的引号,我该怎么办?关于创建此命令以接受为其提供的任何密码,无论该命令中是否带有1或7种引号?

IE可以在不影响密码的情况下用三引号引起来吗?

1 个答案:

答案 0 :(得分:-1)

在评论的帮助下找到答案。问题是(如前所述)没有引号引起来的字符串。因此,如果直接构建参数字符串而不是使用shlex拆分参数字符串,它将自动转义引号/斜杠。例如:

def build_list(string):
    return string.split(" ")

# build_list("""slappasswd -s c;_pr8\\E0L)ec*\'E._rPBH?"4i9Tne""")
#<= ['slappasswd', '-s', 'c;_pr8\\\\E0L)ec*\\\'E._rPBH?"4i9Tne']

一旦一切都正确地逃脱了,您可以继续运行它,它将正常工作。