Advertisement
Izya12

NetXpert/get_lldp_neighbors.py

Apr 29th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # ---------------------------------------------------------------------
  3. # Nateks.NetXpert.get_lldp_neighbors
  4. # ---------------------------------------------------------------------
  5. # Copyright (C) 2007-2017 The NOC Project
  6. # See LICENSE for details
  7. # ---------------------------------------------------------------------
  8.  
  9. # Python modules
  10. import re
  11. # NOC modules
  12. from noc.core.script.base import BaseScript
  13. from noc.sa.interfaces.igetlldpneighbors import IGetLLDPNeighbors
  14. from noc.lib.validators import is_int, is_ipv4, is_ipv6, is_mac
  15. from noc.sa.interfaces.base import MACAddressParameter, IPv4Parameter
  16.  
  17.  
  18. class Script(BaseScript):
  19.     name = "Nateks.NetXpert.get_lldp_neighbors"
  20.     interface = IGetLLDPNeighbors
  21.  
  22.     rx_summary_split = re.compile(r"^Device-ID.+?\n",
  23.                                   re.MULTILINE | re.IGNORECASE)
  24.     rx_s_line = re.compile(
  25.         r"^\S+\s*(?P<local_if>(?:Fas|Gig|Te)\d+[\d\/\.]*)")
  26.     rx_chassis_id = re.compile(
  27.         r"^chassis id:\s*(?P<id>\S+)", re.MULTILINE | re.IGNORECASE)
  28.     rx_remote_port = re.compile(
  29.         "^port id:\s*(?P<remote_if>.+?)\s*$", re.MULTILINE | re.IGNORECASE)
  30.     rx_enabled_caps = re.compile(
  31.         "^enabled capabilities:\s*(?P<caps>\S*)",
  32.         re.MULTILINE | re.IGNORECASE)
  33.     rx_system = re.compile(
  34.         r"^system name:\s*(?P<name>\S+)", re.MULTILINE | re.IGNORECASE)
  35.     rx_descr = re.compile(r"^port description:\s*(?P<descr>.+)$", re.MULTILINE)
  36.  
  37.     def execute(self):
  38.         r = []
  39.         try:
  40.             v = self.cli("show lldp neighbors")
  41.         except self.CLISyntaxError:
  42.             raise self.NotSupportedError()
  43.         if v.startswith("%"):
  44.             return []
  45.         v = self.rx_summary_split.split(v)[1]
  46.         lldp_interfaces = []
  47.         for line in v.splitlines():
  48.             line = line.strip()
  49.             if not line:
  50.                 break
  51.             match = self.rx_s_line.match(line)
  52.             if not match:
  53.                 continue
  54.             lldp_interfaces += [match.group("local_if")]
  55.         for local_if in lldp_interfaces:
  56.             i = {
  57.                 "local_interface": local_if,
  58.                 "neighbors": []
  59.             }
  60.             try:
  61.                 v = self.cli("sh lldp neighbors interface %s" % local_if)
  62.             except self.CLISyntaxError:
  63.                 raise self.NotSupportedError()
  64.             match = self.re_search(self.rx_remote_port, v)
  65.             remote_port = match.group("remote_if")
  66.             remote_port_subtype = 1
  67.             if is_ipv4(remote_port):
  68.                 remote_port = IPv4Parameter().clean(remote_port)
  69.                 remote_port_subtype = 4
  70.             elif is_mac(remote_port):
  71.                 remote_port = MACAddressParameter().clean(remote_port)
  72.                 remote_port_subtype = 3
  73.             elif is_int(remote_port):
  74.                 remote_port_subtype = 7
  75.             n = {
  76.                 "remote_port": remote_port,
  77.                 "remote_port_subtype": remote_port_subtype,
  78.                 "remote_chassis_id_subtype": 4
  79.             }
  80.             match = self.rx_descr.search(v)
  81.             if match:
  82.                 n["remote_port_description"] = match.group("descr")
  83.             match = self.rx_chassis_id.search(v)
  84.             if not match:
  85.                 continue
  86.             n["remote_chassis_id"] = match.group("id")
  87.             cap = 0
  88.             match = self.rx_enabled_caps.search(v)
  89.             if match:
  90.                 for c in match.group("caps").split(","):
  91.                     c = c.strip()
  92.                     if c:
  93.                         cap |= {
  94.                             "O": 1, "P": 2, "B": 4,
  95.                             "W": 8, "R": 16, "T": 32,
  96.                             "C": 64, "S": 128
  97.                         }[c]
  98.             n["remote_capabilities"] = cap
  99.             match = self.rx_system.search(v)
  100.             if match:
  101.                 n["remote_system_name"] = match.group("name")
  102.             if is_ipv4(n["remote_chassis_id"]) \
  103.                or is_ipv6(n["remote_chassis_id"]):
  104.                 n["remote_chassis_id_subtype"] = 5
  105.             elif is_mac(n["remote_chassis_id"]):
  106.                 pass
  107.             else:
  108.                 n["remote_chassis_id_subtype"] = 7
  109.             i["neighbors"] += [n]
  110.             r += [i]
  111.         return r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement