fixes functions and adds cli support

- implements cli
- fixes get_bgp_neighbors #44
- fixes get_lldp_neighbors(multiple neighbors)
This commit is contained in:
Marcos Buzo
2021-08-12 20:07:54 +02:00
parent a5d3b94663
commit 72ed825c1d

View File

@@ -472,12 +472,14 @@ class VyOSDriver(NetworkDriver):
"""
output = self.device.send_command("show ip bgp summary")
output = output.split("\n")
match = re.search(r".* router identifier (\d+\.\d+\.\d+\.\d+), local AS number (\d+)",
output[0])
match = re.search(
r"bgp router identifier (\d+\.\d+\.\d+\.\d+), local as number (\d+) vrf-id (\d+)",
output,
re.MULTILINE | re.IGNORECASE
)
if not match:
return {}
router_id = match.group(1)
local_as = int(match.group(2))
@@ -486,13 +488,24 @@ class VyOSDriver(NetworkDriver):
bgp_neighbor_data["global"]["router_id"] = router_id
bgp_neighbor_data["global"]["peers"] = {}
# delete the header and empty element
bgp_info = [i.strip() for i in output[6:-2] if i]
re_neighbor = re.compile(
'^([\w\d:\.]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\w+)'
)
for bgp_line in output.split("\n"):
match = re_neighbor.search(bgp_line)
if not match:
continue
for i in bgp_info:
if len(i) > 0:
peer_id, bgp_version, remote_as, msg_rcvd, msg_sent, table_version, \
in_queue, out_queue, up_time, state_prefix = i.split()
peer_id = match.group(1)
bgp_version = match.group(2)
remote_as = match.group(3)
msg_rcvd = match.group(4)
msg_sent = match.group(5)
table_version = match.group(6)
in_queue = match.group(7)
out_queue = match.group(8)
up_time = match.group(9)
state_prefix = match.group(10)
is_enabled = "(Admin)" not in state_prefix
@@ -586,27 +599,28 @@ class VyOSDriver(NetworkDriver):
return uptime
def get_lldp_neighbors(self):
# Multiple neighbors per port are not implemented
# The show lldp neighbors commands lists port descriptions, not IDs
ret = []
output = self.device.send_command("show lldp neighbors detail")
pattern = r'''(?s)Interface: +(?P<interface>\S+), [^\n]+
.+?
+SysName: +(?P<hostname>\S+)
.+?
+PortID: +ifname (?P<port>\S+)'''
regex = '{},.+?{}.+?{}'.format(
"Interface:\s+(\S+)",
"SysName:\s+(\S+)",
"PortID:\s+ifname (\S+)"
)
re_lldp = re.compile(
regex,
re.MULTILINE|re.DOTALL
)
match = re_lldp.findall(output)
if not match:
return []
def _get_interface(match):
return [
{
'hostname': match.group('hostname'),
'port': match.group('port'),
}
]
return {
match.group('interface'): _get_interface(match)
for match in re.finditer(pattern, output)
}
for ocurrence in match:
ret.append({
'hostname' : ocurrence[0],
'port' : ocurrence[1],
'interface' : ocurrence[2]
})
return ret
def get_interfaces_counters(self):
# 'rx_unicast_packet', 'rx_broadcast_packets', 'tx_unicast_packets',
@@ -958,3 +972,17 @@ class VyOSDriver(NetworkDriver):
config = config[:config.rfind('\n')]
self.device.exit_config_mode()
return config
def cli(self, commands):
cli_output = {}
if not isinstance(commands, list):
raise TypeError("Please enter a valid list of commands!")
for command in commands:
try:
cli_output[command] = self.device.send_command(command)
except Exception:
cli_output[command] = 'error running command'
return cli_output