Esempio n. 1
0
    def cli(self, output=None):
        if output is None:
            out = self.device.execute(self.cli_command)
        else:
            out = output

        # initialize result dict
        result_dict = {}

        # Channel group 1 neighbors
        p1 = re.compile(r'^\s*Channel +group +(?P<channel_group>[\d]+)')

        # Port           System ID             Port Number     Age         Flags
        # Gi0/0/1         00127,6487.88af.b840  0x2              28s        FA
        p2 = re.compile(
            r'^\s*(?P<interface>[\w/]+) +(?P<system_id>[\w,.]+) +(?P<port_num>[\w]+)'
            ' +(?P<age>[\d]+)s +(?P<flags>[\w]+)$')

        # Port Priority        Oper Key        Port State
        # 100                  0x1             0x3F
        p3 = re.compile(
            r'^\s*(?P<lacp_port_priority>[\d.]+) +(?P<oper_key>[\w]+) +(?P<port_state>[\w]+)$'
        )

        # Activity:   Timeout:   Aggregation:   Synchronization:
        # Active      Short      Yes            Yes
        p4 = re.compile(
            r'^\s*(?P<activity>[\w]+) +(?P<timeout>Long|Short)'
            ' +(?P<aggregatable>[\w]+) +(?P<synchronization>[\w]+)$')

        # Collecting:   Distributing:   Defaulted:   Expired:
        # Yes           Yes             No           No
        p5 = re.compile(
            r'^\s*(?P<collecting>Yes|No) +(?P<distributing>[\w]+) +(?P<defaulted>[\w]+) +(?P<expired>[\w]+)$'
        )

        for line in out.splitlines():
            line = line.rstrip()

            m = p1.match(line)
            if m:
                group = m.groupdict()
                name = 'Port-channel' + group["channel_group"]
                intf_dict = result_dict.setdefault('interfaces',
                                                   {}).setdefault(name, {})
                intf_dict.update({'name': name})
                intf_dict.update({'protocol': 'lacp'})
                continue

            m = p2.match(line)
            if m:
                group = m.groupdict()
                interface = Common.convert_intf_name(group["interface"])
                member_dict = intf_dict.setdefault('members', {}).setdefault(
                    interface, {})
                member_dict.update({'interface': interface})
                member_dict.update({'system_id': group['system_id']})
                member_dict.update({'port_num': int(group['port_num'], 0)})
                member_dict.update({'age': int(group['age'])})
                member_dict.update({'flags': group['flags']})
                continue

            m = p3.match(line)
            if m:
                group = m.groupdict()
                member_dict.update(
                    {'lacp_port_priority': int(group['lacp_port_priority'])})
                member_dict.update({'oper_key': int(group['oper_key'], 0)})
                member_dict.update({'port_state': int(group['port_state'], 0)})
                continue

            m = p4.match(line)
            if m:
                group = m.groupdict()
                member_dict.update({'activity': group['activity']})
                member_dict.update({'timeout': group['timeout']})
                member_dict.update(
                    {'aggregatable': group['aggregatable'] == 'Yes'})
                member_dict.update(
                    {'synchronization': group['synchronization'] == 'Yes'})
                continue

            m = p5.match(line)
            if m:
                group = m.groupdict()
                member_dict.update(
                    {'collecting': group['collecting'] == 'Yes'})
                member_dict.update(
                    {'distributing': group['distributing'] == 'Yes'})
                member_dict.update({'defaulted': group['defaulted'] == 'Yes'})
                member_dict.update({'expired': group['expired'] == 'Yes'})
                continue

        return result_dict
Esempio n. 2
0
    def cli(self, vrf="", af="", output=None):
        if output is None:
            if vrf:
                if af:
                    cmd = self.cli_command[0].format(vrf=vrf, af=af)
                else:
                    cmd = self.cli_command[1].format(vrf=vrf)
            else:
                if af:
                    cmd = self.cli_command[2].format(af=af)
                else:
                    cmd = self.cli_command[3]

            out = self.device.execute(cmd)
        else:
            out = output

        if not vrf:
            vrf = 'default'

        route = interface = next_hop = ""

        result_dict = {}

        # regex

        # No routes in this topology
        p = re.compile(r'^\s*No routes in this topology$')

        # VRF: default Table Id: 0xe0000000 AFI: IPv4 SAFI: Unicast
        p1 = re.compile(
            r'^\s*VRF: +(?P<vrf>[\w]+) +Table +Id: +(?P<table_id>[\w]+) +AFI: +(?P<af>[\w]+)'
            ' +SAFI: +(?P<safi>[\w]+)$')

        # Prefix/Len          Interface                Nexthop             Object              Explicit-path       Metrics
        # 2001:1:1:1::1/128   GigabitEthernet0_0_0_3   2001:10:1:2::1      None                None                [0/0/1/0/1]
        #             GigabitEthernet0_0_0_0   None                None                None                [0/4096/1/0/1]
        # Prefix/Len          Interface                Nexthop             Object         Explicit-path       Metrics          Local-Label
        # 172.16.0.89/32      TenGigE0_0_1_2           None                None          None                [0/4096/1/0/1]    No label    Path is configured at Sep 11 08:29:25.605
        # 10.00.00.0/00       Bundle-Ether2.25         10.01.02.03         None                None                [0/0/1/0/1]
        p2 = re.compile(
            r'^(?P<route>[\d\s\/\.\:]+)?(?P<interface>[a-zA-Z][\w\/\.-]+) '
            r'+(?P<nexthop>[\w\/\.\:]+) +(?P<object>[\w]+) '
            r'+(?P<explicit_path>[\w]+) +(?P<metrics>[\w\/\[\]]+)'
            r'(\s+(?P<local_label>[\w\s]+?))?'
            r'(\s+(?P<path_event>(Path|Last).*))?$')

        # Path is installed into RIB at Dec  7 21:52:00.853
        p3 = re.compile(
            r'^\s*(?P<path_event>Path +is +installed +into +RIB +at +(?P<date>[\S\s]+))$'
        )

        # Path is configured at Dec  7 21:47:43.624
        p3_1 = re.compile(
            r'^\s*(?P<path_event>Path +is +configured +at +(?P<date>[\S\s]+))$'
        )

        # Path is removed from RIB at Dec  7 21:47:43.624
        p3_2 = re.compile(
            r'^\s*(?P<path_event>Path +is +removed +from +RIB +at +(?P<date>[\S\s]+))$'
        )

        # Last RIB event is at Dec  7 21:47:43.624
        p3_3 = re.compile(
            r'^\s*(?P<path_event>Last +RIB +event +is +at +(?P<date>[\S\s]+))$'
        )

        # Path version: 1, Path status: 0x21
        p4 = re.compile(
            r'^\s*Path +version: +(?P<path_version>[\d]+), +Path +status: +(?P<path_status>[\w]+)$'
        )

        # Path has best tag: 0
        p5 = re.compile(r'^\s*Path +has +best +tag: +(?P<tag>[\d]+)$')

        for line in out.splitlines():
            if line:
                line = line.strip()
            else:
                continue

            m = p.match(line)
            if m:
                continue

            m = p1.match(line)
            if m:
                vrf = m.groupdict()['vrf']
                af = m.groupdict()['af'].lower()
                table_id = m.groupdict()['table_id']
                safi = m.groupdict()['safi'].lower()

                af_dict = result_dict.setdefault('vrf', {}).setdefault(vrf, {}).\
                            setdefault('address_family', {}).setdefault(af, {})

                af_dict['safi'] = safi.lower()
                af_dict['table_id'] = table_id
                continue

            m = p2.match(line)

            if m:
                next_hop = ""

                if m.groupdict()['route']:
                    route = m.groupdict()['route'].strip()
                    index = 1
                else:
                    index += 1

                if m.groupdict()['interface'] and 'none' not in m.groupdict(
                )['interface'].lower():
                    interface = Common.convert_intf_name(
                        m.groupdict()['interface'].replace('_', '/'))
                if m.groupdict()['nexthop'] and 'none' not in m.groupdict(
                )['nexthop'].lower():
                    next_hop = m.groupdict()['nexthop']

                if m.groupdict()['metrics']:
                    metrics_value = m.groupdict()['metrics'].strip('[').strip(
                        ']').split('/')
                    metrics = int(metrics_value[4])
                    preference = int(metrics_value[2])

                if m.groupdict()['object'] and 'none' not in m.groupdict(
                )['object'].lower():
                    object = m.groupdict()['object']

                if m.groupdict(
                )['explicit_path'] and 'none' not in m.groupdict(
                )['explicit_path'].lower():
                    explicit_path = m.groupdict()['explicit_path']

                local_label = m.groupdict()['local_label']
                path_event = m.groupdict()['path_event']

                route_dict = af_dict.setdefault('routes',
                                                {}).setdefault(route, {})
                route_dict['route'] = route
                hop_dict = route_dict.setdefault('next_hop', {})

                if not next_hop:

                    intf_dict = hop_dict.setdefault('outgoing_interface', {}).\
                                         setdefault(interface, {})
                    intf_dict['outgoing_interface'] = interface

                    intf_dict['metrics'] = metrics
                    intf_dict['preference'] = preference

                    if m.groupdict(
                    )['explicit_path'] and 'none' not in m.groupdict(
                    )['explicit_path'].lower():
                        intf_dict['explicit_path'] = explicit_path
                    if m.groupdict()['object'] and 'none' not in m.groupdict(
                    )['object'].lower():
                        intf_dict['track'] = int(object)
                    if local_label:
                        intf_dict['local_label'] = local_label
                    if path_event:
                        intf_dict['path_event'] = path_event

                else:
                    idx_dict = hop_dict.setdefault('next_hop_list',
                                                   {}).setdefault(index, {})

                    idx_dict['index'] = index
                    idx_dict['next_hop'] = next_hop.strip()

                    if m.groupdict(
                    )['interface'] and 'none' not in m.groupdict(
                    )['interface'].lower():
                        idx_dict['outgoing_interface'] = interface

                    idx_dict['metrics'] = metrics
                    idx_dict['preference'] = preference

                    if m.groupdict(
                    )['explicit_path'] and 'none' not in m.groupdict(
                    )['explicit_path'].lower():
                        idx_dict['explicit_path'] = explicit_path
                    if m.groupdict()['object'] and 'none' not in m.groupdict(
                    )['object'].lower():
                        idx_dict['track'] = int(object)
                    if local_label:
                        idx_dict['local_label'] = local_label
                    if path_event:
                        idx_dict['path_event'] = path_event
                continue

            m = p3.match(line)
            if m:
                active = True
                path_event = m.groupdict()['path_event']
                if not next_hop:
                    intf_dict['active'] = active
                    intf_dict['path_event'] = path_event
                else:
                    idx_dict['active'] = active
                    idx_dict['path_event'] = path_event
                continue

            m = p3_1.match(line)
            if m:
                active = False
                path_event = m.groupdict()['path_event']
                if not next_hop:
                    intf_dict['active'] = active
                    intf_dict['path_event'] = path_event
                else:
                    idx_dict['active'] = active
                    idx_dict['path_event'] = path_event
                continue

            m = p3_2.match(line)
            if m:
                active = False
                path_event = m.groupdict()['path_event']
                if not next_hop:
                    intf_dict['active'] = active
                    intf_dict['path_event'] = path_event
                else:
                    idx_dict['active'] = active
                    idx_dict['path_event'] = path_event
                continue

            m = p3_3.match(line)
            if m:
                path_event = m.groupdict()['path_event']
                if not next_hop:
                    intf_dict['path_event'] = path_event
                else:
                    idx_dict['path_event'] = path_event
                continue

            m = p4.match(line)
            if m:
                path_version = int(m.groupdict()['path_version'])
                path_status = m.groupdict()['path_status']
                if not next_hop:
                    intf_dict['path_version'] = path_version
                    intf_dict['path_status'] = path_status
                else:
                    idx_dict['path_version'] = path_version
                    idx_dict['path_status'] = path_status
                continue

            m = p5.match(line)
            if m:
                tag = m.groupdict()['tag']
                if not next_hop:
                    intf_dict['tag'] = int(tag)
                else:
                    idx_dict['tag'] = int(tag)
                continue

        return result_dict
    def cli(self, name, output=None):
        if output is None:
            cmd = self.cli_command.format(name=name)
            out = self.device.execute(cmd)
        else:
            out = output

        # Init vars
        ret_dict = {}
        index = 0

        # Cache type:                               Normal (Platform cache)
        p1 = re.compile(r'^Cache +type: +(?P<cache_type>[\S\s]+)$')

        # Cache size:                                   16
        p2 = re.compile(r'^Cache +size: +(?P<cache_size>\d+)$')

        # Current entries:                               1
        p3 = re.compile(r'^Current +entries: +(?P<current_entries>\d+)$')

        # High Watermark:                                1
        p4 = re.compile(r'^High +Watermark: +(?P<high_water_mark>\d+)$')

        # Flows added:                                   1
        p5 = re.compile(r'^Flows +added: +(?P<flows_added>\d+)$')

        # Flows aged:                                   0
        p6 = re.compile(r'^Flows +aged: +(?P<flows_aged>\d+)$')

        # - Inactive timeout    (    15 secs)         15
        # - Event aged                                 0
        # - Watermark aged                             6
        # - Emergency aged                             0
        p7 = re.compile(
            r'^- +(?P<key>[\S\s]+?)( +\( +(?P<secs>\d+) +secs\))? +(?P<value>\d+)$'
        )

        # 0   (DEFAULT)   192.168.189.254    192.168.189.253    Null   Te0/0/0.1003     2
        p8 = re.compile(
            r'^(?P<ip_vrf_id_input>\d+ +\(\S+\)) +(?P<ipv4_src_addr>\S+) '
            r'+(?P<ipv4_dst_addr>\S+) +(?P<intf_input>\S+) '
            r'+(?P<intf_output>\S+) +(?P<pkts>\d+)$')

        # IP VRF ID INPUT:           0          (DEFAULT)
        p9 = re.compile(r'^IP VRF ID INPUT: +(?P<id>[\S\s]+)$')

        # IPV4 SOURCE ADDRESS:       192.168.189.254
        p10 = re.compile(r'^IPV4 SOURCE ADDRESS: +(?P<src>\S+)$')

        # IPV4 DESTINATION ADDRESS:  192.168.189.253
        p11 = re.compile(r'^IPV4 DESTINATION ADDRESS: +(?P<dst>\S+)$')

        # interface input:           Null
        p12 = re.compile(r'^interface input: +(?P<input>\S+)$')

        # interface output:          Te0/0/0.1003
        p13 = re.compile(r'^interface output: +(?P<output>\S+)$')

        # counter packets:           3
        p14 = re.compile(r'^counter packets: +(?P<pkts>\d+)$')

        # entry_dict intializes on p8 or p9 condition
        # but some output doesn't match these conditions.
        # this variable checks the entry_dict created
        entry_dict_created = False

        for line in out.splitlines():
            line = line.strip()

            # Cache type:                               Normal (Platform cache)
            m = p1.match(line)
            if m:
                group = m.groupdict()
                ret_dict.update({'cache_type': group['cache_type']})
                continue

            # Cache size:                                   16
            m = p2.match(line)
            if m:
                group = m.groupdict()
                ret_dict.update({'cache_size': int(group['cache_size'])})
                continue

            # Current entries:                               1
            m = p3.match(line)
            if m:
                group = m.groupdict()
                ret_dict.update(
                    {'current_entries': int(group['current_entries'])})
                continue

            # High Watermark:                                1
            m = p4.match(line)
            if m:
                group = m.groupdict()
                ret_dict.update(
                    {'high_water_mark': int(group['high_water_mark'])})
                continue

            # Flows added:                                   1
            m = p5.match(line)
            if m:
                group = m.groupdict()
                ret_dict.update({'flows_added': int(group['flows_added'])})
                continue

            # Flows aged:                                   0
            m = p6.match(line)
            if m:
                group = m.groupdict()
                aged_dict = ret_dict.setdefault('flows_aged', {})
                aged_dict.update({'total': int(group['flows_aged'])})
                continue

            # - Inactive timeout    (    15 secs)         15
            m = p7.match(line)
            if m:
                group = m.groupdict()
                key = group['key'].lower().replace(' ', '_')
                aged_dict.update({key: int(group['value'])})

                secs = group['secs']
                if secs:
                    aged_dict.update({key + '_secs': int(secs)})
                continue

            # 0   (DEFAULT)   192.168.189.254    192.168.189.253    Null   Te0/0/0.1003     2
            m = p8.match(line)
            if m:
                index += 1
                group = m.groupdict()
                entry_dict = ret_dict.setdefault('entries',
                                                 {}).setdefault(index, {})

                entry_dict.update(
                    {'ip_vrf_id_input': group['ip_vrf_id_input']})
                entry_dict.update({'ipv4_src_addr': group['ipv4_src_addr']})
                entry_dict.update({'ipv4_dst_addr': group['ipv4_dst_addr']})
                entry_dict.update({
                    'intf_input':
                    Common.convert_intf_name(group['intf_input'])
                })
                entry_dict.update({
                    'intf_output':
                    Common.convert_intf_name(group['intf_output'])
                })
                entry_dict.update({'pkts': int(group['pkts'])})
                entry_dict_created = True
                continue

            # IP VRF ID INPUT:           0          (DEFAULT)
            m = p9.match(line)
            if m:
                index += 1
                group = m.groupdict()
                entry_dict = ret_dict.setdefault('entries',
                                                 {}).setdefault(index, {})
                entry_dict.update({'ip_vrf_id_input': group['id']})
                entry_dict_created = True
                continue

            # IPV4 SOURCE ADDRESS:       192.168.189.254
            m = p10.match(line)
            if m:
                group = m.groupdict()

                if not entry_dict_created:
                    index += 1
                    entry_dict = ret_dict.setdefault('entries',
                                                     {}).setdefault(index, {})

                entry_dict.update({'ipv4_src_addr': group['src']})

                continue

            # IPV4 DESTINATION ADDRESS:  192.168.189.253
            m = p11.match(line)
            if m:
                group = m.groupdict()
                entry_dict.update({'ipv4_dst_addr': group['dst']})
                continue

            # interface input:           Null
            m = p12.match(line)
            if m:
                group = m.groupdict()
                entry_dict.update(
                    {'intf_input': Common.convert_intf_name(group['input'])})
                continue

            # interface output:          Te0/0/0.1003
            m = p13.match(line)
            if m:
                group = m.groupdict()
                entry_dict.update(
                    {'intf_output': Common.convert_intf_name(group['output'])})
                continue

            # counter packets:           3
            m = p14.match(line)
            if m:
                group = m.groupdict()
                entry_dict.update({'pkts': int(group['pkts'])})
                continue

        return ret_dict
    def cli(self, interface=None, output=None):
        '''Module that parsers the output'''

        if not output:
            # get output from device
            out = self.device.execute(self.cli_command.format(interface=interface))
        else:
            out = output

        #Interface: Gi1/0/12
        p1 = re.compile(r'^Interface:\s+(?P<Interface>[\w/\-.]+)')

                # Method              Source            Template-Name
        # ------              ------            -------------
        # dynamic             User              cts_dot1x
        # static              User              port-config
        p2 = re.compile(
            r"^(?P<method>(static|dynamic))\s+"
            r"(?P<source>\w+)\s+"
            r"(?P<interface_template_name>[\w_\-.]+)"
        )
        # Template-Name                                    Source            Bound-To-MAc
        # -------------                                    ------            ----------------
        # DefaultCriticalAuthVlan_SRV_TEMPLATE             User               68-3B-78-7A-2C-90
        # DefaultCriticalVoice_SRV_TEMPLATE                User               68-3B-78-7A-2C-90
        p3 = re.compile(
            r"^(?P<service_template_name>[\w_]+)\s+"
            r"(?P<source>[\w\-]+)\s+"
            r"(?P<mac>[A-F0-9\-]+)"
        )

        result_dict = {}
        for line in out.splitlines():
            line = line.strip()
            
            m = p1.match(line)

            if m:
                group_dict_1 = m.groupdict()
                Interface = group_dict_1.pop('Interface')
                converted_interface = Common.convert_intf_name(Interface)
                continue

            m = p2.match(line)

            if m:
                group_dict_2 = m.groupdict()
                method = group_dict_2.pop('interface_template_name')
                method_dict = result_dict.setdefault(converted_interface, {}).\
                setdefault('interface_templates', {}).setdefault(method, {})
                method_dict.update({k: v.lower() for k, v in group_dict_2.items()})
                continue

            m = p3.match(line)

            if m:
                group_dict_3 = m.groupdict()
                if (len(group_dict_3['mac'])) > 12:
                    mac_address = group_dict_3.pop('service_template_name')
                    mac_dict = result_dict.setdefault(converted_interface, {}).\
                    setdefault('service_templates', {}).setdefault(mac_address, {})
                    mac_dict.update({k: v.lower() for k, v in group_dict_3.items()})
                continue

        return result_dict