2012-11-01 9 views
6

Sto utilizzando Fabric per automatizzare una distribuzione. In questo processo utilizzo la funzione prompt per chiedere all'utente qualche input. In particolare, ho bisogno di chiedere una password e mi piacerebbe nascondere il valore che l'utente inserisce, come ad esempio usando Python getpass. Mi piacerebbe usare prompt a causa della gestione degli argomenti key e validate args.Ottieni una password dall'utente in Fabric, non riecheggiando il valore

Esiste un modo incorporato Fabric per farlo, oppure è necessario modificare I prompt source (eventualmente inviare una richiesta pull)?

risposta

6

Potreste essere in grado di utilizzare prompt_for_password in 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 

Sembra che questo è il modo in tessuto recupera password per ssh, hanno quindi impostare questo per env utilizza:

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

Key è facilmente sostituibile impostando env, ma sembra che tu debba convalidare te stesso ...

Problemi correlati