2012-11-01 24 views
6

Tôi đang sử dụng Fabric để tự động triển khai. Trong quá trình này, tôi sử dụng hàm prompt để hỏi người dùng một số đầu vào. Đặc biệt, tôi cần yêu cầu mật khẩu và tôi muốn ẩn giá trị mà người dùng nhập vào, như sử dụng Python getpass. Tôi muốn sử dụng prompt vì việc xử lý keyvalidate args.Nhận mật khẩu từ người dùng trong Fabric, không lặp lại giá trị

Có cách tạo sẵn vải để làm như vậy hay tôi có thay đổi prompt source (cuối cùng gửi yêu cầu kéo) không?

Trả lời

6

Bạn có thể có thể sử dụng prompt_for_password trong fabric.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 

Dường như đây là cách vải lấy mật khẩu cho ssh, Họ sau đó thiết lập này để env sử dụng:

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

Key là một cách dễ dàng có thể thay thế bằng cách đặt env, nhưng có vẻ như bạn có thể phải xác thực chính mình ...

Các vấn đề liên quan