Example #1
0
def get_username_and_password(device, cmdline_username, config):
    """Get a username and a password for device.

    The username and password are selected from user input,
    config files and device information.
    """
    username_list = get_username_list(cmdline_username, config)
    service_list = get_username_service_list(config)
    password = select_device_password(device, username_list, service_list)
    username = None
    if password and password.attributes.get('username'):
        username = password.attributes.get('username')
        password = password.password
    if not username:
        if len(username_list) == 0:
            username = None
            while not username:
                username = utils.read_response('Enter username')
        elif len(username_list) == 1:
            username = username_list[0]
        else:
            options = [utils.PicklistOption(u) for u in username_list]
            username = utils.pick_from_list(options, 'Select username').option
    if not password:
        password = utils.read_password(verify=False)
    return username, password
Example #2
0
def select_device_password(device, usernames, services):
    """Select a password object from a device based a list of usernames.
    
    If a single match is found that will be used, otherwise the user
    is prompted to make a selection.
    """
    match = None
    all_passwords = device.listChildren(include = ['password'])
    if not all_passwords:
        return None
    matched_u_and_s = []
    matched_u = []
    matched_s = []
    for password in all_passwords:
        username = password.attributes.get('username')
        service = password.attributes.get('description')
        if username in usernames and (not service or service in services):
            matched_u_and_s.append(password)
        if username in usernames:
            matched_u.append(password)
        if service in services:
            matched_s.append(password)
    matched_passwords = matched_u_and_s or matched_u or matched_s
    if not matched_passwords:
        matched_passwords = all_passwords
    if len(matched_passwords) == 1:
        ret = matched_passwords[0]
    else:
        options = []
        options.append(utils.PicklistOption('Abort', None))
        for password in matched_passwords:
            username = password.attributes.get('username', '')
            description = password.attributes.get('description')
            if username and description:
                optionstr = '%s - %s' % (username, description)
            elif username:
                optionstr = username
            elif description:
                optionstr = description
            else:
                optionstr = 'Unknown password %s' % password.oid
            option = utils.PicklistOption(optionstr, password)
            options.append(option)
        ret = utils.pick_from_list(options, 'Password').value
        if ret is None:
            sys.exit(1)
    return ret
Example #3
0
def select_device_hostname_or_ip(device, config):
    """Return the hostname/ip used for connecting to a device.
    
    If the config option connect-ip is set we try to find a valid
    ip-address to connect to, otherwise the device name is used.
    """
    hostname = None
    if config.getBool('connect-ip', False):
        networks = device.listNetworks(include_ranges=False,
                include_interfaces=True, only_hosts=True)
        networks = [n.strAddress() for n in networks if not n.attributes.get('secondary', False)]
        if len(networks) == 1:
            hostname = networks[0]
        elif len(networks) > 1:
            options = [utils.PicklistOption(n)  for n in networks]
            hostname = utils.pick_from_list(options, 'Select IP').option
    if not hostname:
        hostname = device.attributes.get('name')
    return hostname