예제 #1
0
    def get_bgp_neighbors(self):

        families = ["ipv4", "ipv6"]
        terms = dict({"accepted_prefixes": "accepted", "sent_prefixes": "announced"})
        command_sum = "get router info bgp sum"
        command_detail = "get router info bgp neighbor {}"
        command_received = "get router info bgp neighbors {} received-routes | grep prefixes "
        peers = dict()

        bgp_sum = self.execute_command_with_vdom(command_sum)

        re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
        neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)}

        self.device.load_config("router bgp")

        for neighbor, parameters in neighbors.iteritems():
            logger.debug("NEW PEER")
            neigh_conf = self.device.running_config["router bgp"]["neighbor"]["{}".format(neighbor)]

            neighbor_dict = peers.get(neighbor, dict())

            if not neighbor_dict:
                neighbor_dict["local_as"] = int(bgp_sum[0].split()[7])
                neighbor_dict["remote_as"] = int(neigh_conf.get_param("remote-as"))
                neighbor_dict["is_up"] = "never" != parameters[7] or False
                neighbor_dict["is_enabled"] = neigh_conf.get_param("shutdown") != "enable" or False
                neighbor_dict["description"] = u""
                neighbor_dict["uptime"] = convert_uptime_string_seconds(parameters[7])
                neighbor_dict["address_family"] = dict()
                neighbor_dict["address_family"]["ipv4"] = dict()
                neighbor_dict["address_family"]["ipv6"] = dict()

            detail_output = [x.lower() for x in self.execute_command_with_vdom(command_detail.format(neighbor))]
            m = re.search("remote router id (.+?)\n", "\n".join(detail_output))
            if m:
                neighbor_dict["remote_id"] = unicode(m.group(1))
            else:
                raise Exception("cannot find remote router id for %s" % neighbor)

            for family in families:
                # find block
                x = detail_output.index(" for address family: {} unicast".format(family))
                block = detail_output[x:]

                for term, fortiname in terms.iteritems():
                    text = self._search_line_in_lines("%s prefixes" % fortiname, block)
                    t = [int(s) for s in text.split() if s.isdigit()][0]
                    neighbor_dict["address_family"][family][term] = t

                received = self.execute_command_with_vdom(command_received.format(neighbor))[0].split()
                if len(received) > 0:
                    neighbor_dict["address_family"][family]["received_prefixes"] = received[-1]
                else:
                    # Soft-reconfig is not enabled
                    neighbor_dict["address_family"][family]["received_prefixes"] = 0
            peers[neighbor] = neighbor_dict

        return {"global": {"router_id": unicode(bgp_sum[0].split()[3]), "peers": peers}}
예제 #2
0
파일: fortios.py 프로젝트: taijiji/napalm
    def get_bgp_neighbors(self):

        families = ['ipv4', 'ipv6']
        terms = dict({'accepted_prefixes': 'accepted', 'sent_prefixes': 'announced'})
        command_sum = 'get router info bgp sum'
        command_detail = 'get router info bgp neighbor {}'
        command_received = 'get router info bgp neighbors {} received-routes | grep prefixes '
        peers = dict()

        bgp_sum = self.device.execute_command(command_sum)
        re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
        neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)}

        self.device.load_config('router bgp')

        for neighbor, parameters in neighbors.iteritems():
            logger.debug('NEW PEER')
            neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)]

            neighbor_dict = peers.get(neighbor, dict())

            if not neighbor_dict:
                neighbor_dict['local_as'] = int(bgp_sum[0].split()[7])
                neighbor_dict['remote_as'] = int(neigh_conf.get_param('remote-as'))
                neighbor_dict['is_up'] = 'never' != parameters[7] or False
                neighbor_dict['is_enabled'] = neigh_conf.get_param('shutdown') != 'enable' or False
                neighbor_dict['description'] = u''
                neighbor_dict['uptime'] = convert_uptime_string_seconds(parameters[7])
                neighbor_dict['address_family'] = dict()
                neighbor_dict['address_family']['ipv4'] = dict()
                neighbor_dict['address_family']['ipv6'] = dict()

            detail_output = [x.lower() for x in self.device.execute_command(command_detail.format(neighbor))]
            m = re.search('remote router id (.+?)\n', '\n'.join(detail_output))
            if m:
                neighbor_dict['remote_id'] = unicode(m.group(1))
            else:
                raise Exception('cannot find remote router id for %s' % neighbor)

            for family in families:
                # find block
                x = detail_output.index(' for address family: {} unicast'.format(family))
                block = detail_output[x:]

                for term, fortiname in terms.iteritems():
                    text = self._search_line_in_lines('%s prefixes' % fortiname, block)
                    t = [int(s) for s in text.split() if s.isdigit()][0]
                    neighbor_dict['address_family'][family][term] = t

                received = self.device.execute_command(
                    command_received.format(neighbor))[0].split()
                if len(received) > 0:
                    neighbor_dict['address_family'][family]['received_prefixes'] = received[-1]
                else:
                    # Soft-reconfig is not enabled
                    neighbor_dict['address_family'][family]['received_prefixes'] = 0
            peers[neighbor] = neighbor_dict

        return {
            'global': {
                'router_id': unicode(bgp_sum[0].split()[3]),
                'peers': peers
            }
        }
예제 #3
0
    def get_bgp_neighbors(self):

        families = ['ipv4', 'ipv6']
        terms = dict({
            'accepted_prefixes': 'accepted',
            'sent_prefixes': 'announced'
        })
        command_sum = 'get router info bgp sum'
        command_detail = 'get router info bgp neighbor {}'
        command_received = 'get router info bgp neighbors {} received-routes | grep prefixes '
        peers = dict()

        bgp_sum = self._execute_command_with_vdom(command_sum)

        re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
        neighbors = {
            n.split()[0]: n.split()[1:]
            for n in bgp_sum if re.match(re_neigh, n)
        }

        self.device.load_config('router bgp')

        for neighbor, parameters in neighbors.items():
            logger.debug('NEW PEER')
            neigh_conf = self.device.running_config['router bgp']['neighbor'][
                '{}'.format(neighbor)]

            neighbor_dict = peers.get(neighbor, dict())

            if not neighbor_dict:
                neighbor_dict['local_as'] = int(bgp_sum[0].split()[7])
                neighbor_dict['remote_as'] = int(
                    neigh_conf.get_param('remote-as'))
                neighbor_dict['is_up'] = 'never' != parameters[7] or False
                neighbor_dict['is_enabled'] = neigh_conf.get_param(
                    'shutdown') != 'enable' or False
                neighbor_dict['description'] = u''
                neighbor_dict['uptime'] = convert_uptime_string_seconds(
                    parameters[7])
                neighbor_dict['address_family'] = dict()
                neighbor_dict['address_family']['ipv4'] = dict()
                neighbor_dict['address_family']['ipv6'] = dict()

            detail_output = [
                x.lower() for x in self._execute_command_with_vdom(
                    command_detail.format(neighbor))
            ]
            m = re.search('remote router id (.+?)\n', '\n'.join(detail_output))
            if m:
                neighbor_dict['remote_id'] = py23_compat.text_type(m.group(1))
            else:
                raise Exception('cannot find remote router id for %s' %
                                neighbor)

            for family in families:
                # find block
                x = detail_output.index(
                    ' for address family: {} unicast'.format(family))
                block = detail_output[x:]

                for term, fortiname in terms.items():
                    text = self._search_line_in_lines(
                        '%s prefixes' % fortiname, block)
                    t = [int(s) for s in text.split() if s.isdigit()][0]
                    neighbor_dict['address_family'][family][term] = t

                received = self._execute_command_with_vdom(
                    command_received.format(neighbor))[0].split()
                if len(received) > 0:
                    neighbor_dict['address_family'][family][
                        'received_prefixes'] = received[-1]
                else:
                    # Soft-reconfig is not enabled
                    neighbor_dict['address_family'][family][
                        'received_prefixes'] = 0
            peers[neighbor] = neighbor_dict

        return {
            'global': {
                'router_id': py23_compat.text_type(bgp_sum[0].split()[3]),
                'peers': peers
            }
        }