示例#1
0
def delete(user, host, port, key_ids):
    ssh_controller = SSHController(user, host, port)

    try:
        remote_keys = [k for k in get_authorized_keys(ssh_controller) if k]
    except NoSuchFileError:
        logging.critical('{c.user}@{c.host}:{c.port} - error: not found authorized_keys'
                            .format(c=ssh_controller))
        sys.exit(1)

    try:
        for key_id in [int(i) for i in sorted(key_ids, reverse=True)]:
            del remote_keys[key_id - 1]
    except IndexError:
        logging.critical('{c.user}@{c.host}:{c.port} - error: not found key indexes'
                            .format(c=ssh_controller))
        sys.exit(1)

    scp_controller = SCPController(user, host, port)
    scp_controller.password = ssh_controller.password
    set_authorized_keys(scp_controller, remote_keys)
示例#2
0
def add(user, host, port, key_files):
    local_keys = load_local_keys(key_files)

    ssh_controller = SSHController(user, host, port)

    try:
        remote_keys = get_authorized_keys(ssh_controller)
    except sh.ErrorReturnCode_1:
        sys.exit(1)
    except NoSuchFileError:
        create_authorized_keys_file(ssh_controller)
        remote_keys = []

    new_keys = []
    already_keys = []

    for key_file in key_files:
        key = local_keys[key_file]

        if key not in remote_keys:
            new_keys.append(key)
        else:
            already_keys.append(key_file)

    if already_keys:
        logging.info(
            '{c.user}@{c.host}:{c.port} - already in authorized_keys: "{0}"'.
            format('", "'.join(already_keys), c=ssh_controller))

    if new_keys:
        keys = remote_keys + new_keys

        scp_controller = SCPController(user, host, port)
        scp_controller.password = ssh_controller.password

        try:
            set_authorized_keys(scp_controller, keys)
        except sh.ErrorReturnCode_1:
            sys.exit(1)
示例#3
0
def add(user, host, port, key_files):
    local_keys = load_local_keys(key_files)

    ssh_controller = SSHController(user, host, port)

    try:
        remote_keys = get_authorized_keys(ssh_controller)
    except sh.ErrorReturnCode_1:
        sys.exit(1)
    except NoSuchFileError:
        create_authorized_keys_file(ssh_controller)
        remote_keys = []

    new_keys = []
    already_keys = []

    for key_file in key_files:
        key = local_keys[key_file]

        if key not in remote_keys:
            new_keys.append(key)
        else:
            already_keys.append(key_file)

    if already_keys:
        logging.info('{c.user}@{c.host}:{c.port} - already in authorized_keys: "{0}"'
                        .format('", "'.join(already_keys), c=ssh_controller))

    if new_keys:
        keys = remote_keys + new_keys

        scp_controller = SCPController(user, host, port)
        scp_controller.password = ssh_controller.password

        try:
            set_authorized_keys(scp_controller, keys)
        except sh.ErrorReturnCode_1:
            sys.exit(1)
示例#4
0
def delete(user, host, port, key_ids):
    ssh_controller = SSHController(user, host, port)

    try:
        remote_keys = [k for k in get_authorized_keys(ssh_controller) if k]
    except NoSuchFileError:
        logging.critical(
            '{c.user}@{c.host}:{c.port} - error: not found authorized_keys'.
            format(c=ssh_controller))
        sys.exit(1)

    try:
        for key_id in [int(i) for i in sorted(key_ids, reverse=True)]:
            del remote_keys[key_id - 1]
    except IndexError:
        logging.critical(
            '{c.user}@{c.host}:{c.port} - error: not found key indexes'.format(
                c=ssh_controller))
        sys.exit(1)

    scp_controller = SCPController(user, host, port)
    scp_controller.password = ssh_controller.password
    set_authorized_keys(scp_controller, remote_keys)