add_quagga_user.py 2.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/env python3

# Copyright (C) 2018 Simon Redman <sredman@cs.utah.edu>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import ssh_helper

import argparse
import getpass
import json

24
ADDUSER_COMMAND = "sudo adduser --disabled-password --gecos quagga quagga"
25 26


27
def add_quagga_user_to_network(sessions):
28 29 30
    """
    Add the quagga user to every host in the network

31
    :param sessions: logged-in sessions on which to run commands
32 33 34 35
    :param ssh_options: options to pass to ssh, as per pxssh documentation
    :return: list of passwords which were used to log in to the servers, where empty string means no password was used
    """
    commands = [ADDUSER_COMMAND for i in range(len(hostnames))]
36 37 38
    outputs = ssh_helper.run_commands_on_many_hosts(sessions, commands)

    return outputs
39 40 41 42 43 44


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Send a command to create a user named \'quagga\' on every host in the provided NetJSON")
    parser.add_argument("--in-file", action='store', type=str, required=True,
                        help="Path to the NetJSON file to parse")
45 46
    parser.add_argument("--username", action='store', type=str, default=getpass.getuser(),
                        help="Username to use on all hosts. Defaults to current user's username")
47 48 49 50 51 52 53

    args = parser.parse_args()

    with open(args.in_file, 'r') as netjson_file:
        netjson = json.load(netjson_file)
        hostnames = [node['properties']['management-ip'] for node in netjson['nodes']]
        usernames = [args.username for i in range(len(hostnames))]
54 55
        sessions, passwords = ssh_helper.log_in_many_sessions(hostnames, usernames)
        outputs = add_quagga_user_to_network(sessions)
56
        pass