【问题标题】:Get a password from the user in Fabric, not echoing the value从 Fabric 中的用户那里获取密码,而不是回显该值
【发布时间】:2012-10-22 05:16:29
【问题描述】:

我正在使用Fabric 来自动化部署。在这个过程中,我使用prompt 函数来询问用户一些输入。特别是我需要询问密码,并且我想隐藏用户输入的值,例如使用 Python getpass。由于keyvalidate args 的处理,我想使用prompt

是否有 Fabric 内置方法可以这样做,或者我是否必须更改 prompt source(最终发送拉取请求)?

【问题讨论】:

    标签: python passwords hide user-input fabric


    【解决方案1】:

    您也许可以在fabric.network 中使用prompt_for_password

    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
    

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

    def set_password(password):
        from fabric.state import env
        env.password = env.passwords[env.host_string] = password
    

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

    【讨论】:

      猜你喜欢
      • 2015-07-29
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 2011-11-30
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多