Ejemplo n.º 1
0
def remove_user_public_key(username, hosts, user_public_key):
    """
    :param username: The username of the user on the remote machines
    :param hosts: A list of servers to remove the user's public ssh key from
    :param user_public_key: The key to remove
    :return: A dict of whether operation succeeded on each host
    """

    if username == 'root':
        return {}

    module_args = 'state=absent user=%s key=\"%s\"' % (username, user_public_key)
    runner = VinzRunner(hosts=hosts, module_name='authorized_key', module_args=module_args)
    response = runner.run()
    results = {}
    for host in hosts:
        host_result = {}
        if not host in response['contacted']:
            #host is dark
            host_result['success'] = False
            host_result['error'] = "Could not contact host %s" % (host)
        else:
            result = response['contacted'][host]
            if 'failed' in result:
                #something went wrong
                host_result['success'] = False
                host_result['error'] = result['msg'] or ""
            else:
                host_result['success'] = True
        results[host] = host_result

    return results
Ejemplo n.º 2
0
Archivo: user.py Proyecto: mpdavis/vinz
def get_users_on_host(hostname):
    """
    :param hostname: The hostname of the server to get a list of users from.
    :return A list of usernames on the machine
    """

    runner = VinzRunner(hostname,
                        module_name='command',
                        module_args='cat /etc/passwd')
    results = runner.run()

    if not hostname in results['contacted']:
        raise DarkServerException("Host %s could not be contacted." % hostname)
    try:
        output = results['contacted'][hostname]['stdout']
    except Exception, e:
        output = ''
Ejemplo n.º 3
0
def get_authorized_keys_for_host(host, usernames):
    """
    :param host: The hostname of the server to get authorized_keys files from
    :param usernames: A list of usernames to get authorized_keys files for
    :return A dictionary mapping usernames to all of their authorized_keys

    {
        "root": ['ssh-rsa AAAAkjfk...', 'ssh_rsa AAAfmfkdm'],
        "vinz": ['ssh-rsa AAABdfdf...', 'ssh_rsa AAAfmfkdm'],
    }
    """

    command_string = 'cat ~%s/.ssh/authorized_keys'

    key_files = dict()
    errors = dict()

    for username in usernames:
        runner = VinzRunner([host], module_name='command', module_args=command_string % username)
        response = runner.run()

        if not host in response['contacted']:
            continue

        std_out = response['contacted'][host]['stdout']
        std_err = response['contacted'][host]['stderr']

        if not std_out:
            errors[username] = std_err
            continue

        key_files[username] = std_out

    results = dict()
    for username, keys in key_files.iteritems():
        results[username] = keys.splitlines()

    return results
Ejemplo n.º 4
0
Archivo: user.py Proyecto: mpdavis/vinz
def add_user(username, hosts):
    """
    :param username: The username of the user to be added to the remote machines
    :param hosts: A list of servers to add the user to
    """

    if username == 'root':
        return

    if not isinstance(username, basestring):
        raise ValueError("Username must be a string")

    runner = VinzRunner(hosts,
                        module_name='user',
                        module_args='name=%s shell=/bin/bash' % username)
    results = runner.run()

    if not isinstance(hosts, list):
        hosts = [hosts]

    contacted = results['contacted']
    for host in hosts:
        if not contacted.get(host, None):
            raise DarkServerException("Host %s could not be contacted." % host)