Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_mac(self):
"""
Tests the helper function ```mac```:
* check if raises AddrFormatError when invalid MAC
* check if MAC address returned as expected
"""
self.assertTrue(HAS_NETADDR)
# test that raises AddrFormatError when wrong format
self.assertRaises(AddrFormatError, napalm.base.helpers.mac, "fake")
self.assertEqual(napalm.base.helpers.mac("0123456789ab"), "01:23:45:67:89:AB")
self.assertEqual(napalm.base.helpers.mac("0123.4567.89ab"), "01:23:45:67:89:AB")
self.assertEqual(napalm.base.helpers.mac("123.4567.89ab"), "01:23:45:67:89:AB")
if not interface_neighbors:
# in case of empty infos
continue
# it is provided a list of neighbors per interface
for neighbor in interface_neighbors:
if interface not in lldp_neighbors_out.keys():
lldp_neighbors_out[interface] = []
capabilities = neighbor.get("systemCapabilities", {})
available_capabilities = self._transform_lldp_capab(capabilities.keys())
enabled_capabilities = self._transform_lldp_capab(
[capab for capab, enabled in capabilities.items() if enabled]
)
remote_chassis_id = neighbor.get("chassisId", "")
if neighbor.get("chassisIdType", "") == "macAddress":
remote_chassis_id = napalm.base.helpers.mac(remote_chassis_id)
neighbor_interface_info = neighbor.get("neighborInterfaceInfo", {})
lldp_neighbors_out[interface].append(
{
"parent_interface": interface, # no parent interfaces
"remote_port": neighbor_interface_info.get("interfaceId", ""),
"remote_port_description": neighbor_interface_info.get(
"interfaceDescription", ""
),
"remote_system_name": neighbor.get("systemName", ""),
"remote_system_description": neighbor.get(
"systemDescription", ""
),
"remote_chassis_id": remote_chassis_id,
"remote_system_capab": available_capabilities,
"remote_system_enable_capab": enabled_capabilities,
}
for k, v in self.device.run_commands(commands)[0].get("vrfs").items()
if not vrf or k == vrf
for neighbor in v.get("ipV4Neighbors", [])
]
except pyeapi.eapilib.CommandError:
return []
for neighbor in ipv4_neighbors:
interface = str(neighbor.get("interface"))
mac_raw = neighbor.get("hwAddress")
ip = str(neighbor.get("address"))
age = float(neighbor.get("age"))
arp_table.append(
{
"interface": interface,
"mac": napalm.base.helpers.mac(mac_raw),
"ip": napalm.base.helpers.ip(ip),
"age": age,
}
)
return arp_table
)
for mac_entry in mac_table_raw:
raw_mac = mac_entry.get("disp_mac_addr")
interface = str(mac_entry.get("disp_port"))
try:
vlan = int(mac_entry.get("disp_vlan"))
except ValueError:
vlan = 0
active = True
static = mac_entry.get("disp_is_static") != "0"
moves = 0
last_move = 0.0
mac_table.append(
{
"mac": napalm.base.helpers.mac(raw_mac),
"interface": interface,
"vlan": vlan,
"active": active,
"static": static,
"moves": moves,
"last_move": last_move,
}
)
return mac_table
"""Return proper data for mac address fields."""
if mac_type.lower() in ["self", "static", "system"]:
static = True
if vlan.lower() == "all":
vlan = 0
if (
interface.lower() == "cpu"
or re.search(r"router", interface.lower())
or re.search(r"switch", interface.lower())
):
interface = ""
else:
static = False
return {
"mac": napalm.base.helpers.mac(mac),
"interface": self._canonical_int(interface),
"vlan": int(vlan),
"static": static,
"active": True,
"moves": -1,
"last_move": -1.0,
}
except IndexError:
protocol = ""
if "admin" in status.lower():
is_enabled = False
else:
is_enabled = True
if protocol:
is_up = bool("up" in protocol)
else:
is_up = bool("up" in status)
break
mac_addr_regex = r"^\s+Hardware.+address\s+is\s+({})".format(MAC_REGEX)
if re.search(mac_addr_regex, line):
mac_addr_match = re.search(mac_addr_regex, line)
mac_address = napalm.base.helpers.mac(mac_addr_match.groups()[0])
descr_regex = r"^\s+Description:\s+(.+?)$"
if re.search(descr_regex, line):
descr_match = re.search(descr_regex, line)
description = descr_match.groups()[0]
speed_regex = r"^\s+MTU\s+(\d+).+BW\s+(\d+)\s+([KMG]?b)"
if re.search(speed_regex, line):
speed_match = re.search(speed_regex, line)
mtu = int(speed_match.groups()[0])
speed = speed_match.groups()[1]
speedformat = speed_match.groups()[2]
speed = float(speed)
if speedformat.startswith("Kb"):
speed = speed / 1000.0
elif speedformat.startswith("Gb"):
"static": False,
"active": True,
"moves": 0,
"last_move": 0.0,
}
for mac_table_entry in mac_table_items:
mac_entry = default_values.copy()
mac_entry.update({elem[0]: elem[1] for elem in mac_table_entry[1]})
mac = mac_entry.get("mac")
# JUNOS returns '*' for Type = Flood
if mac == "*":
continue
mac_entry["mac"] = napalm.base.helpers.mac(mac)
mac_address_table.append(mac_entry)
return mac_address_table
def get_ipv6_neighbors_table(self):
"""Return the IPv6 neighbors table."""
ipv6_neighbors_table = []
ipv6_neighbors_table_raw = junos_views.junos_ipv6_neighbors_table(self.device)
ipv6_neighbors_table_raw.get()
ipv6_neighbors_table_items = ipv6_neighbors_table_raw.items()
for ipv6_table_entry in ipv6_neighbors_table_items:
ipv6_entry = {elem[0]: elem[1] for elem in ipv6_table_entry[1]}
ipv6_entry["mac"] = napalm.base.helpers.mac(ipv6_entry.get("mac"))
ipv6_entry["ip"] = napalm.base.helpers.ip(ipv6_entry.get("ip"))
ipv6_neighbors_table.append(ipv6_entry)
return ipv6_neighbors_table
except IndexError:
protocol = ""
if "admin" in status.lower():
is_enabled = False
else:
is_enabled = True
if protocol:
is_up = bool("up" in protocol)
else:
is_up = bool("up" in status)
break
mac_addr_regex = r"^\s+Hardware.+address\s+is\s+({})".format(MAC_REGEX)
if re.search(mac_addr_regex, line):
mac_addr_match = re.search(mac_addr_regex, line)
mac_address = napalm.base.helpers.mac(mac_addr_match.groups()[0])
descr_regex = r"^\s+Description:\s+(.+?)$"
if re.search(descr_regex, line):
descr_match = re.search(descr_regex, line)
description = descr_match.groups()[0]
speed_regex = r"^\s+MTU\s+\d+.+BW\s+(\d+)\s+([KMG]?b)"
if re.search(speed_regex, line):
speed_match = re.search(speed_regex, line)
speed = speed_match.groups()[0]
speedformat = speed_match.groups()[1]
speed = float(speed)
if speedformat.startswith("Kb"):
speed = speed / 1000.0
elif speedformat.startswith("Gb"):
speed = speed * 1000