Advertisement
Izya12

get_lldp_neighbors.py

Apr 22nd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # ---------------------------------------------------------------------
  3. # InfiNet.WANFlexX.get_lldp_neighbors
  4. # ---------------------------------------------------------------------
  5. # Copyright (C) 2007-2018 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.core.mac import MAC
  15.  
  16.  
  17. class Script(BaseScript):
  18.     name = "InfiNet.WANFlexX.get_lldp_neighbors"
  19.     interface = IGetLLDPNeighbors
  20.     rx_int = re.compile(r"Table on\s*(?P<interface>\S+)",re.MULTILINE)
  21.     rx_ChassisID = re.compile(r"ChassisID:    \|\s*(?P<chassis_id>\S+)\s\((?P<chassis_subtype>[a-z]*)",re.MULTILINE)
  22.     rx_PortID = re.compile(r"PortID:       \|\s*(?P<port_id>\S+)\s\((?P<port_subtype>[a-zN]*)",re.MULTILINE)
  23.     rx_mac = re.compile(r"^[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}$")
  24.  
  25.     def execute_cli(self):
  26.         result = []
  27.         try:
  28.             lldp = self.cli("lldp report")
  29.         except self.CLISyntaxError:
  30.             raise self.NotSupportedError()
  31.  
  32.         for match in self.rx_int.finditer(lldp):
  33.             result += [{
  34.                 "local_interface": match.group("interface"),
  35.                 "neighbors": [{
  36.                     "remote_chassis_id_subtype": self.rx_ChassisID.search(lldp).group("chassis_subtype"),
  37.                     "remote_chassis_id": self.rx_ChassisID.search(lldp).group("chassis_id"),
  38.                     "remote_port_subtype": {
  39.                         "Interface alias": 1,
  40.                         "Port component": 2,
  41.                         "Local": 7,
  42.                         "ifName": 5,
  43.                         "mac": 3
  44.                     }[self.rx_PortID.search(lldp).group("port_subtype")],
  45.                     "remote_port": self.rx_PortID.search(lldp).group("port_id")
  46.                 }]
  47.             }]
  48.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement