def setUp(self):
        self.manager = ConnectionManager()
        self.ip = "127.0.0.1"
        self.port = 5561
        self.object_to_send = {'a': 1, 'b': 2, 'c': {'x': 7, 'y': 8, 'z': 9}}
        self.object_received = None

        self.ReceivingSocket = socket.socket(socket.AF_INET,
                                             socket.SOCK_STREAM)
        self.ReceivingSocket.bind((self.ip, self.port))
        self.ReceiveThread = threading.Thread(target=self.receiving_thread)
        self.ReceiveThread.daemon = True
        self.ReceiveThread.start()

        print self.manager.__repr__()
        print self.manager
class OpenListeningPortTest(unittest.TestCase):
    def setUp(self):
        self.manager = ConnectionManager()
        self.ip = "127.0.0.1"
        self.port = 5560
        self.object_to_send = {'a': 1, 'b': 2, 'c': {'x': 7, 'y': 8, 'z': 9}}
        self.object_received = None

    def test_open_listening_port(self):
        self.ListeningThread = threading.Thread(target=self.listening_thread)
        self.ListeningThread.daemon = True
        self.ListeningThread.start()

        self.SendThread = threading.Thread(target=self.sending_thread)
        self.SendThread.daemon = True
        self.SendThread.start()

        sleep(0.1)  # Rather than messing about with locks.
        self.failUnlessEqual(self.object_received, self.object_to_send)

    def listening_thread(self):
        self.manager.new_connection_callback(self.receiving_thread)
        self.manager.open_listening_port(self.ip, self.port)

    def sending_thread(self):
        self.SendingSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.SendingSock.connect((self.ip, self.port))

        data_raw = pickle.dumps(self.object_to_send)
        self.SendingSock.sendall(struct.pack('>i', len(data_raw)) + data_raw)
        self.SendingSock.close()

    def receiving_thread(self, cxn):
        self.ReceivingConnection = cxn
        data = self.ReceivingConnection.recv()
        self.object_received = data
        self.ReceivingConnection.close()
Example #3
0
    def execute(self):

        # Set logging level
        logging_format = "[ %(levelname)-8s ][ %(asctime)s,%(msecs)03d ]:  %(message)s"
        datetime_format = "%H:%M:%S"

        if self.args.log_level == 'debug':
            level = level_paramiko_transport = logging.DEBUG
            logging_format = "[%(levelname)8s][%(asctime)s,%(msecs)03d]:" \
                             "%(name)s:%(funcName)s(){l.%(lineno)d}:  %(message)s"
            datetime_format = "%Y-%m-%d %H:%M:%S"
        elif self.args.log_level == 'info':
            level = logging.INFO
            level_paramiko_transport = logging.ERROR
        elif self.args.log_level == 'error':
            level = level_paramiko_transport = logging.ERROR
        else:
            level = level_paramiko_transport = logging.CRITICAL

        logging.basicConfig(stream=sys.stderr,
                            level=level,
                            format=logging_format,
                            datefmt=datetime_format)
        logging.getLogger("paramiko.transport").setLevel(
            level_paramiko_transport)

        # Provide basic information if logging required
        logging.debug("Started")

        logging.info("Level of debugging: {}".format(self.args.log_level))
        logging.info("System running: {} ({})".format(platform.system(),
                                                      os.name))
        logging.info("Output directory: {}".format(self.args.output_dir))
        logging.info("JSON output file: {}".format(self.args.output_json))
        logging.info("Database output: {}".format(self.args.output_db))
        logging.info("Allow other then 'show'-commands: {}".format(
            self.args.allow_no_show))
        logging.info("Credential file: {}".format(self.args.credentials))
        logging.info("Credential reset: {}".format(self.args.reset))
        logging.info("Settings file: {}".format(self.args.setting_file))
        logging.info("Device list file: {}".format(self.args.device_list))
        logging.info("Command list file: {}".format(self.args.command_list))

        logging.info("Initial setup loading...")

        s = utils.read_from_json_file(self.args.setting_file)
        d = None

        # Receiving device list if device file is given use that.
        devices = {}
        if self.args.device_list:
            source = 'file'
            with open(self.args.device_list) as device_file:
                hosts_list = device_file.read().splitlines()
                index = 0
                for host in hosts_list:
                    devices[index] = {
                        'NAME': host,
                        'IP': host,
                        'PROTOCOL': self.args.connection
                    }
                    index += 1
        elif self.args.output_db:

            # Setting up data base manager
            d = DatbaseManager(database=s['SETTINGS']['MYSQL_DATABASE'],
                               user=s['SETTINGS']['MYSQL_USER'],
                               password=s['SETTINGS']['MYSQL_PASSWORD'],
                               sql_server=s['SETTINGS']['MYSQL_SERVER'])

            source = 'database'
            d.connect()
            devices = d.get_device_list()
            d.disconnect()
        else:
            logging.critical("Must select in-/output method!")
            sys.exit(10)

        logging.info("Devices from {} loaded! (Total: {})".format(
            source, len(devices)))

        # Read commands from file:
        try:
            with open(self.args.command_list) as device_file:
                commands_list = device_file.read().splitlines()
        except IOError as e:
            logging.error("I/O error({0}): {1}".format(e.errno, e.strerror))
            raise

        # Connect SSH tunnel to jump node!
        logging.debug('Creating SSH tunnel connector...')
        j = ConnectionManager.JumpCollection(s['SETTINGS']['PATH'],
                                             s["JUMPSERVERS"])
        logging.info("Connected to Jumpservers!")

        # Setting up connection and output collector objects
        c = ConnectionManager.TunnelConnectionAgent(
            am=accountmgr.AccountManager(config_file=self.args.credentials,
                                         reset=self.args.reset),
            jumpservers=j,
            ssh_command=s['SETTINGS']['SSH_COMMAND'],
            telnet_command=s['SETTINGS']['TELNET_COMMAND'],
            timeout=s['SETTINGS']['TIMEOUT'])

        # Setup host manager (Output collector)
        h = HostManager.HostManagment(db=d)

        # Collection of data
        output = {}
        logging.info("Performing device captures...")
        for device in devices:
            # Add host to host manager
            h.add_host(devices[device]['NAME'],
                       db_id=device,
                       ipv4=devices[device]['IP'])

            # Connect to end device
            connection = c.connect(
                devices[device]['IP'],
                connection_protocol=devices[device]['PROTOCOL'])

            if connection:
                # Set unlimited terminal length
                c.terminal_lenth_cisco(devices[device]['NAME'])

                # Send commands and collect output
                output[device] = {}
                for command in commands_list:
                    if connection:
                        # Only show commands are allowed!
                        connection, out = c.send_command(
                            devices[device]['NAME'],
                            command,
                            allow_more_show=self.args.allow_no_show)
                        if out:
                            h.add_command(devices[device]['NAME'], command,
                                          out)

                # Disconnect from end node gracefully
                if connection:
                    c.disconnect()
                logging.info("Finished data collection for {}!".format(
                    devices[device]['NAME']))

        # Disconnect from jump nodes
        logging.debug('Terminating SSH tunnel to connector...')
        j.disconnect_jumpserver_chain()
        logging.info("Disconnected from jumpservers!")

        # Output options!

        # Output to files in output directory
        if self.args.output_dir:
            utils.dir_check(self.args.output_dir)
            h.write_to_txt_files(self.args.output_dir)
            logging.info("Saved output to text files in:{}!".format(
                self.args.output_dir))

        # Write to JSON output
        if self.args.output_json:
            h.write_to_json(self.args.output_json)
            logging.info("Saved output to JSON file:{}!".format(
                self.args.output_json))

        # Send output to SQL server
        if self.args.output_db:
            if not self.args.device_list:
                h.write_to_db()
                logging.info("Saved output to database!")
            else:
                logging.error(
                    "Not saving output to database! Can only save output to database"
                    " if devices are retreived from database!")

        logging.debug("Script ended")
        sys.exit()
Example #4
0
def main():
    args = option_parser()

    logging.debug("Started")

    logging.info("Level of debugging: {}".format(args.debug))
    logging.info("System running: {} ({})".format(platform.system(), os.name))
    logging.info("Output directory: {}".format(args.output_dir))
    logging.info("JSON output file: {}".format(args.output_json))
    logging.info("Credential file: {}".format(args.credentials))
    logging.info("Credential reset: {}".format(args.reset))
    logging.info("Settings file: {}".format(args.setting_file))
    logging.info("Device list file: {}".format(args.device_list))
    logging.info("Command list file: {}".format(args.command_list))

    # Open files that are required.
    try:
        with open(args.device_list) as device_file:
            hosts_list = device_file.read().splitlines()
        with open(args.command_list) as device_file:
            commands_list = device_file.read().splitlines()
    except IOError as e:
        logging.error("I/O error({0}): {1}".format(e.errno, e.strerror))
        sys.exit(10)

    s = utils.read_from_json_file(args.setting_file)

    # Create list with jumpservers as Device objects
    jumpservers = []

    if 'PATH' in s['SETTINGS']:
        if len(s['SETTINGS']['PATH']) > 0:
            for j in s['SETTINGS']['PATH']:
                if j in s['JUMPSERVERS']:
                    jumpservers.append(HostManager.Device(j,
                                                          prompt=s['JUMPSERVERS'][j]['PROMPT'],
                                                          ssh=s['JUMPSERVERS'][j]['SSH_COMMAND'],
                                                          telnet=s['JUMPSERVERS'][j]['TELNET_COMMAND'],
                                                          connection_type=s['JUMPSERVERS'][j]['CONNECTION_TYPE'],
                                                          timeout=s['JUMPSERVERS'][j]['TIMEOUT'],
                                                          port=s['JUMPSERVERS'][j]['PORT']))
                else:
                    logging.critical("Jumpserver {} could not be found in settings!".format(j))

    # Setting up connection and output collector objects
    d = ConnectionManager.ConnectionAgent(am=accountmgr.AccountManager(
                                              config_file=args.credentials, reset=args.reset),
                                          client_connection_type=args.connection,
                                          ssh_command=s['SETTINGS']['SSH_COMMAND'],
                                          telnet_command=s['SETTINGS']['TELNET_COMMAND'],
                                          timeout=s['SETTINGS']['TIMEOUT'],
                                          shell=s['SETTINGS']['SHELL'],
                                          jumpservers=jumpservers)
    h = HostManager.HostManagment()


    # Walk through list of hosts, connect, execute command and save to object.
    for host in hosts_list:
        h.add_host(host)
        d.host_connect(host)
        d.cisco_term_len()
        for command in commands_list:
            h.add_command(host, command, d.send_command(command))
        d.disconnect_host()

    # Saving output if neeeded.
    if args.output_dir:
        utils.dir_check(args.output_dir)
        h.write_to_txt_files(args.output_dir)
    if args.output_json:
        h.write_to_json(args.output_json)

    logging.debug("Script ended")
class OpenSendingText(unittest.TestCase):
    def setUp(self):
        self.manager = ConnectionManager()
        self.ip = "127.0.0.1"
        self.port = 5561
        self.object_to_send = {'a': 1, 'b': 2, 'c': {'x': 7, 'y': 8, 'z': 9}}
        self.object_received = None

        self.ReceivingSocket = socket.socket(socket.AF_INET,
                                             socket.SOCK_STREAM)
        self.ReceivingSocket.bind((self.ip, self.port))
        self.ReceiveThread = threading.Thread(target=self.receiving_thread)
        self.ReceiveThread.daemon = True
        self.ReceiveThread.start()

        print self.manager.__repr__()
        print self.manager

    def test_sending_port(self):
        cxn = self.manager.open_outbound_connection(self.ip, self.port)

        cxn.send(self.object_to_send)
        sleep(0.1)  # Rather than messing about with locks.
        self.failUnlessEqual(self.object_received, self.object_to_send)

    def receiving_thread(self):
        # Based on https://pymotw.com/2/socket/tcp.html,
        # https://code.activestate.com/recipes/408859-socketrecv-three-ways-to-turn-it-into-recvall/
        self.ReceivingSocket.listen(1)

        connection, client_address = self.ReceivingSocket.accept()

        try:

            # Receive the data in small chunks and retransmit it
            while True:
                total_len = 0
                total_data = []
                size = sys.maxint
                size_data = ''
                sock_data = ''
                recv_size = 8192
                while total_len < size:
                    sock_data = connection.recv(recv_size)
                    if not total_data:
                        if len(sock_data) > 4:
                            size_data += sock_data
                            size = struct.unpack('>i', size_data[:4])[0]
                            recv_size = size
                            if recv_size > 524288:
                                recv_size = 524288
                            total_data.append(size_data[4:])
                            total_len = sum([len(i) for i in total_data])
                            data_raw = ''.join(total_data)
                        else:
                            size_data += sock_data

                    else:
                        total_data.append(sock_data)
                        total_len = sum([len(i) for i in total_data])
                        data_raw = ''.join(total_data)

                # Unpickle!
                data = pickle.loads(data_raw)
                self.object_received = data

        finally:
            # Clean up the connection
            connection.close()
 def setUp(self):
     self.manager = ConnectionManager()
     self.ip = "127.0.0.1"
     self.port = 5560
     self.object_to_send = {'a': 1, 'b': 2, 'c': {'x': 7, 'y': 8, 'z': 9}}
     self.object_received = None
    def test_singleton(self):
        firstManager = ConnectionManager()
        secondManager = ConnectionManager()

        self.failUnless(firstManager is secondManager)