从Fabric中获取用户的密码,而不是回显该值

Pao*_*olo 6 python passwords user-input hide fabric

我正在使用Fabric来自动部署.在这个过程中,我使用提示功能向用户询问一些输入.特别是我需要输入密码,我想隐藏用户输入的值,比如使用Python getpass.我想使用prompt因为处理keyvalidateargs.

是否有Fabric内置方式,或者我是否需要更改提示源(最终发送拉取请求)?

dm0*_*514 6

你也许能够使用prompt_for_passwordfabric.network

def prompt_for_password(prompt=None, no_colon=False, stream=None):
    """
    Prompts for and returns a new password if required; otherwise, returns
    None.

    A trailing colon is appended unless ``no_colon`` is True.

    If the user supplies an empty password, the user will be re-prompted until
    they enter a non-empty password.

    ``prompt_for_password`` autogenerates the user prompt based on the current
    host being connected to. To override this, specify a string value for
    ``prompt``.

    ``stream`` is the stream the prompt will be printed to; if not given,
    defaults to ``sys.stderr``.
    """
    from fabric.state import env
    handle_prompt_abort("a connection or sudo password")
    stream = stream or sys.stderr
    # Construct prompt
    default = "[%s] Login password for '%s'" % (env.host_string, env.user)
    password_prompt = prompt if (prompt is not None) else default
    if not no_colon:
        password_prompt += ": "
    # Get new password value
    new_password = getpass.getpass(password_prompt, stream)
    # Otherwise, loop until user gives us a non-empty password (to prevent
    # returning the empty string, and to avoid unnecessary network overhead.)
    while not new_password:
        print("Sorry, you can't enter an empty password. Please try again.")
        new_password = getpass.getpass(password_prompt, stream)
    return new_password
Run Code Online (Sandbox Code Playgroud)

看起来这就是结构为ssh检索密码的方式,然后将其设置为env使用:

def set_password(password):
    from fabric.state import env
    env.password = env.passwords[env.host_string] = password
Run Code Online (Sandbox Code Playgroud)

键可以通过设置轻松替换env,但看起来您可能需要验证自己...