-1

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 我可以用三引号括起来而不影响密码吗?

4

1 回答 1

-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']

一旦一切都正确逃脱,你可以继续运行它,它会工作得很好。

于 2018-06-22T17:45:32.310 回答