Add LLDP auto cabling
This commit is contained in:
parent
0ef0e1c698
commit
1b9bf46762
3 changed files with 154 additions and 4 deletions
42
netbox_agent/lldp.py
Normal file
42
netbox_agent/lldp.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
import subprocess
|
||||
|
||||
|
||||
class LLDP():
|
||||
def __init__(self):
|
||||
self.output = subprocess.getoutput('lldpctl -f keyvalue')
|
||||
self.data = self.parse()
|
||||
|
||||
def parse(self):
|
||||
output_dict = {}
|
||||
for entry in self.output.splitlines():
|
||||
path, value = entry.strip().split("=", 1)
|
||||
path = path.split(".")
|
||||
path_components, final = path[:-1], path[-1]
|
||||
|
||||
current_dict = output_dict
|
||||
for path_component in path_components:
|
||||
current_dict[path_component] = current_dict.get(path_component, {})
|
||||
current_dict = current_dict[path_component]
|
||||
current_dict[final] = value
|
||||
return output_dict
|
||||
|
||||
def get_switch_ip(self, interface):
|
||||
# lldp.eth0.chassis.mgmt-ip=100.66.7.222
|
||||
if self.data['lldp'].get(interface) is None:
|
||||
return None
|
||||
return self.data['lldp'][interface]['chassis']['mgmt-ip']
|
||||
|
||||
def get_switch_port(self, interface):
|
||||
# lldp.eth0.port.descr=GigabitEthernet1/0/1
|
||||
if self.data['lldp'].get(interface) is None:
|
||||
return None
|
||||
return self.data['lldp'][interface]['port']['descr']
|
||||
|
||||
def get_switch_vlan(self, interface):
|
||||
# lldp.eth0.vlan.vlan-id=296
|
||||
if self.data['lldp'].get(interface) is None:
|
||||
return None
|
||||
|
||||
if self.data['lldp'][interface].get('vlan'):
|
||||
return int(self.data['lldp'][interface]['vlan'].replace('vlan-', ''))
|
||||
return None
|
Loading…
Add table
Add a link
Reference in a new issue