Пример #1
0
def _print_host(host, host_type):
    host_str = host['mac']
    if len(host['ipAddresses']) > 0:
        host_str = host_str + ' (' + ', '.join(host['ipAddresses']) + ')'

    print('The ' + UseStyle(host_type, fore='green') + ' host is ' +
          UseStyle(host_str, fore='green'))
Пример #2
0
 def select_dst_host(self):
     host_str_list = [
         host['mac'] + ('(' + ','.join(host['ipAddresses']) +
                        ')' if len(host['ipAddresses']) > 0 else '')
         for host in self.hosts_obj.get_data()
     ]
     cmd_selector = CommandSingleSelector(host_str_list,
                                          'Please select the dst host.')
     selector_str = cmd_selector.get_selector()
     self.dst_mac = selector_str.split('(')[0]
     print('The selected ' + UseStyle('DST', fore='green') + ' host is ' +
           UseStyle(selector_str, fore='green'))
Пример #3
0
 def select_src_host(self):
     host_str_list = [
         host['mac'] + ('(' + ','.join(host['ipAddresses']) +
                        ')' if len(host['ipAddresses']) > 0 else '')
         for host in self.hosts_obj.get_data()
     ]
     # snap_times = get_all_snap_time(self.mars_config)
     cmd_selector = CommandSingleSelector(host_str_list,
                                          'Please select the src host.')
     selector_str = cmd_selector.get_selector()
     self.src_mac = selector_str.split('(')[0]
     print('The selected ' + UseStyle('SRC', fore='green') + ' host is ' +
           UseStyle(selector_str, fore='green'))
Пример #4
0
 def show_online_devices(self):
     print_normal('Show the ' +
                  UseStyle('ONLINE', fore='green', mode='underline') +
                  ' devices.')
     devices_config_obj = DeviceConfigs.initialize(
         mars_config=self.mars_config)
     for device_config in devices_config_obj.get_data():
         print_normal(device_to_line_string(device_config))
Пример #5
0
 def init_online_data(self):
     print('Start to trace the ' +
           UseStyle('ONLINE', fore='green', mode='underline') + ' data')
     self.device_config_obj = DeviceConfigs.initialize(self.mars_config)
     self.hosts_obj = Hosts.initialize(self.mars_config)
     self.group_obj = Groups.initialize(self.mars_config)
     self.flow_obj = Flows.initialize(self.mars_config)
     self.link_obj = Links.initialize(self.mars_config)
Пример #6
0
 def select_gateway(self):
     no_gateway_str = 'NO GATEWAY'
     host_str_list = [
         host['mac'] + ('(' + ','.join(host['ipAddresses']) +
                        ')' if len(host['ipAddresses']) > 0 else '')
         for host in self.hosts_obj.get_data()
     ]
     host_str_list.insert(0, no_gateway_str)
     cmd_selector = CommandSingleSelector(
         host_str_list, 'Please select the gateway host.')
     selector_str = cmd_selector.get_selector()
     if no_gateway_str == selector_str:
         print(UseStyle('NO GATEWAY', fore='green'))
     else:
         self.gateway = selector_str.split('(')[0]
         print('The selected ' + UseStyle('GATEWAY', fore='green') +
               ' is ' + UseStyle(selector_str, fore='green'))
Пример #7
0
    def _print_paths(self,
                     src_host,
                     dst_host,
                     paths,
                     is_dst_gateway=False,
                     is_src_gateway=False):
        print(UseStyle('[GATEWAY]' if is_src_gateway else
                       (src_host['mac'] + '(' +
                        ','.join(src_host['ipAddresses']) + ')'),
                       fore='yellow'),
              end=' ')

        first_link_word = ' X '

        for location in src_host['locations']:
            if location['elementId'] == paths[0].flow['deviceId'] and location[
                    'port'] == paths[0].in_port:
                first_link_word = '==>'

        if first_link_word == ' X ':
            print(UseStyle(first_link_word, fore='red'), end=' ')
        else:
            print(UseStyle(first_link_word, fore='green'), end=' ')

        for path in paths:
            device_name = self.device_config_obj.get_device_name(
                path.flow['deviceId'])

            link_sign = UseStyle(' ==>', fore='green')
            if path.flow['state'] != 'ADDED':
                link_sign = UseStyle(' ==>', fore='red')

            print(UseStyle(
                path.in_port + '| ' + device_name + ' |' + path.out_port,
                fore='yellow',
                mode='underline') + link_sign,
                  end=' ')

        last_path = paths[-1]
        is_last_path_connect_dst = False
        for location in dst_host['locations']:
            if location['elementId'] == last_path.flow[
                    'deviceId'] and location['port'] == last_path.out_port:
                is_last_path_connect_dst = True

        if not is_last_path_connect_dst:
            print(UseStyle(' X ', fore='red'), end=' ')

        print(
            UseStyle('[GATEWAY]' if is_dst_gateway else
                     (dst_host['mac'] + '(' +
                      ','.join(dst_host['ipAddresses']) + ')'),
                     fore='yellow'))
Пример #8
0
    def show_snap_devices(self, snap_time):
        snap_time_str = format_time_string_2_number(snap_time)
        print_normal('Show the ' +
                     UseStyle(snap_time, fore='green', mode='underline') +
                     ' devices.')
        devices_config_obj = DeviceConfigs.initialize_with(
            self.mars_config,
            get_devices_configs(self.mars_config, snap_time_str))

        for device_config in devices_config_obj.get_data():
            print_normal(device_to_line_string(device_config))
Пример #9
0
 def show_online_links(self):
     print_normal('Show the ' +
                  UseStyle('ONLINE', fore='green', mode='underline') +
                  ' links.')
     link_obj = Links.initialize(mars_config=self.mars_config)
     devices_config_obj = DeviceConfigs.initialize(
         mars_config=self.mars_config)
     for device_config in devices_config_obj.get_data():
         print_normal_start('Device Name : ' + device_config['name'],
                            color='yellow')
         for link in link_obj.get_data():
             if link['src']['device'] == device_config['id']:
                 print_normal_center(
                     link_to_line_string(link, devices_config_obj))
         print_normal_end('')
         print_normal('')
Пример #10
0
 def init_snap_data(self, snap_time_str):
     print('Start to trace the ' +
           UseStyle(snap_time_str, fore='green', mode='underline') +
           ' snap data\n')
     snap_time = format_time_string_2_number(snap_time_str)
     self.snap_time = snap_time
     self.device_config_obj = DeviceConfigs.initialize_with(
         self.mars_config, get_devices_configs(self.mars_config, snap_time))
     self.hosts_obj = Hosts.initialize_with(
         self.mars_config, get_host(self.mars_config, snap_time))
     self.group_obj = Groups.initialize_with(
         self.mars_config, get_group(self.mars_config, snap_time))
     self.flow_obj = Flows.initialize_with(
         self.mars_config, get_flow(self.mars_config, snap_time))
     self.link_obj = Links.initialize_with(
         self.mars_config, get_link(self.mars_config, snap_time))
Пример #11
0
    def get_path(self):
        print('')

        hosts = self.hosts_obj.get_data()
        src_host = filter(lambda x: x['mac'] == self.src_mac, hosts)[0]
        dst_host = filter(lambda x: x['mac'] == self.dst_mac, hosts)[0]

        _print_host(src_host, 'SRC')
        _print_host(dst_host, 'DST')

        try:
            if self.gateway is None:
                paths = find_path(self.flow_obj.get_data(),
                                  self.link_obj.get_data(),
                                  self.group_obj.get_data(), src_host,
                                  dst_host)
                print('\nThe Path is : ')
                self._print_paths(src_host, dst_host, paths)
            else:
                gateway = filter(lambda x: x['mac'] == self.gateway, hosts)[0]

                _print_host(gateway, 'GATEWAY')

                paths1 = find_path(self.flow_obj.get_data(),
                                   self.link_obj.get_data(),
                                   self.group_obj.get_data(), src_host,
                                   gateway)
                print('\nThe Path from SRC to GATEWAY is : ')
                self._print_paths(src_host,
                                  gateway,
                                  paths1,
                                  is_dst_gateway=True)
                paths2 = find_path(self.flow_obj.get_data(),
                                   self.link_obj.get_data(),
                                   self.group_obj.get_data(), gateway,
                                   dst_host)

                print('The Path from GATEWAY to DST is : ')
                self._print_paths(gateway,
                                  dst_host,
                                  paths2,
                                  is_src_gateway=True)
        except Exception as e:
            print(UseStyle(e.message, fore='red'))
Пример #12
0
    def show_online_hosts(self):
        print_normal('Show the ' +
                     UseStyle('ONLINE', fore='green', mode='underline') +
                     ' hosts.')
        host_obj = Hosts.initialize(mars_config=self.mars_config)
        devices_config_obj = DeviceConfigs.initialize(
            mars_config=self.mars_config)

        for device_config in devices_config_obj.get_data():
            print_normal_start('Device Name : ' + device_config['name'],
                               color='yellow')
            for host in host_obj.get_data():
                for location in host['locations']:
                    if location['elementId'] == device_config['id']:
                        print_normal_center(
                            host_to_line_string(host, devices_config_obj))

            print_normal_end('')
            print_normal('')
Пример #13
0
    def show_snap_links(self, snap_time_str):
        snap_time = format_time_string_2_number(snap_time_str)
        print_normal('Show the ' +
                     UseStyle(snap_time_str, fore='green', mode='underline') +
                     ' links.')

        link_obj = Links.initialize_with(self.mars_config,
                                         get_link(self.mars_config, snap_time))
        devices_config_obj = DeviceConfigs.initialize_with(
            self.mars_config, get_devices_configs(self.mars_config, snap_time))

        for device_config in devices_config_obj.get_data():
            print_normal_start('Device Name : ' + device_config['name'],
                               color='yellow')
            for link in link_obj.get_data():
                if link['src']['device'] == device_config['id']:
                    print_normal_center(
                        link_to_line_string(link, devices_config_obj))
            print_normal_end('')
            print_normal('')
Пример #14
0
    def show_snap_hosts(self, snap_time_str):
        print_normal('Show the ' +
                     UseStyle(snap_time_str, fore='green', mode='underline') +
                     ' hosts.')

        snap_time = format_time_string_2_number(snap_time_str)
        host_obj = Hosts.initialize_with(self.mars_config,
                                         get_host(self.mars_config, snap_time))
        devices_config_obj = DeviceConfigs.initialize_with(
            self.mars_config, get_devices_configs(self.mars_config, snap_time))

        for device_config in devices_config_obj.get_data():
            print_normal_start('Device Name : ' + device_config['name'],
                               color='yellow')
            for host in host_obj.get_data():
                for location in host['locations']:
                    if location['elementId'] == device_config['id']:
                        print_normal_center(
                            host_to_line_string(host, devices_config_obj))

            print_normal_end('')
            print_normal('')
Пример #15
0
    def _re_color(self, word):
        item = word.replace('<em>', '\033[' + str(GREEN_NUMBER) + 'm')
        item = item.replace('</em>', '\033[0m')

        item = item.replace('INFO',
                            '\033[' + str(GREEN_NUMBER) + 'mINFO\033[0m')
        item = item.replace('WARN',
                            '\033[' + str(YELLOW_NUMBER) + 'mWARN\033[0m')
        item = item.replace('ERROR',
                            '\033[' + str(RED_NUMBER) + 'mERROR\033[0m')

        utc_time_word = item.split('|')[0].strip()

        try:
            local_time = utc_time_str_2_time_stamp(utc_time_word)
            time_word = format_time_stamp_2_string(local_time, is_mill=False)
            time_word_with_mill = time_word + '.' + utc_time_word[-4:-1]
            item = UseStyle(time_word_with_mill,
                            fore='blue') + item[len(utc_time_word):]
            return item
        except Exception as e:
            return word