Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix get_facts, get_interface, get_interface_ip #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions napalm_huawei_vrp/huawei_vrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,29 @@ def get_facts(self):
# os_version/uptime/model
for line in show_ver.splitlines():
if "VRP (R) software" in line:
search_result = re.search(r"\(S\S+\s+(?P<os_version>V\S+)\)", line)
if search_result is not None:
os_version = search_result.group("os_version")

if "HUAWEI" in line and "uptime is" in line:
search_result = re.search(r"S\S+", line)
if search_result is not None:
model = search_result.group(0)
uptime = self._parse_uptime(line)
break
line_process = re.split(" ", line)
os_version = line_process[len(line_process) - 1]
os_version = os_version[:-1]
if "HUAWEI " in line:
if "uptime is" in line:
line_process = re.split("uptime is", line)
model = re.split("HUAWEI", line_process[0])[1].lstrip().rstrip()
uptime = self._parse_uptime(line_process[1])
if "Routing Switch" in model:
model = re.split(" ", model)[0]
elif "Huawei " in line:
if "uptime is" in line:
line_process = re.split("uptime is", line)
model = re.split("Huawei", line_process[0])[1].lstrip().rstrip()
uptime = self._parse_uptime(line_process[1])
if "Router" in model:
model = re.split(" ", model)[0]

# get serial_number,due to the stack have multiple SN, so show it in a list
# 由于堆叠设备会有多少个SN,所以这里用列表展示
re_sn = r"ESN\s+of\s+slot\s+\S+\s+(?P<serial_number>\S+)"
serial_number = re.findall(re_sn, show_esn, flags=re.M)
serial_number = []
for line in show_esn.splitlines():
serial_number.append(re.split(":", line)[1].lstrip().rstrip())

if "sysname " in show_hostname:
_, hostname = show_hostname.split("sysname ")
Expand Down Expand Up @@ -610,6 +618,8 @@ def get_interfaces(self):
re_mac = r"Hardware address is\W+(?P<mac_address>\S+)"
re_speed = r"^Speed\W+(?P<speed>\d+|\w+)"
re_description = r"Description:(?:(?:)|(?P<description>.*))\n"
re_speed_eth_ce = r"Current BW : +(?P<speed>\d+)"
re_speed_eth_ar = r"Current BW: +(?P<speed>\d+)"
re_mtu = r"Maximum Transmit Unit(?:(?:\(L3\))|(?:)) is (?P<int_mtu>\d+)"

new_interfaces = self._separate_section(separator, output)
Expand All @@ -622,6 +632,8 @@ def get_interfaces(self):
msg = "Unexpected interface format: {}".format(interface)
raise ValueError(msg)
intf_name = match_intf.group("intf_name")
if intf_name == "Dialer1:0":
continue
intf_state = match_intf.group("intf_state")
is_enabled = bool("up" in intf_state.lower())

Expand All @@ -648,6 +660,16 @@ def get_interfaces(self):
if speed.isdigit():
speed = float(speed)

match_speed_eth_ar = re.findall(re_speed_eth_ar, interface, flags=re.M)
if match_speed_eth_ar:
if match_speed_eth_ar[0].isdigit():
speed = float(match_speed_eth_ar[0]) * 1000

match_speed_eth_ce = re.findall(re_speed_eth_ce, interface, flags=re.M)
if match_speed_eth_ce:
if match_speed_eth_ce[0].isdigit():
speed = float(match_speed_eth_ce[0]) * 1000

# description matching
description = ""
match = re.search(re_description, interface)
Expand Down Expand Up @@ -726,6 +748,13 @@ def get_interfaces_ip(self):
# v4_interfaces[intf_name] = {}
match_ip = re.findall(re_intf_ip, interface, flags=re.M)

if not match_ip:
re_intf_ip = r"Internet Address is negotiated,\s+(?P<ip_address>\d+.\d+.\d+.\d+)\/(?P<prefix_length>\d+)"
match_ip = re.findall(re_intf_ip, interface, flags=re.M)
if not match_ip:
re_intf_ip = r"Internet Address is allocated by DHCP,\s+(?P<ip_address>\d+.\d+.\d+.\d+)\/(?P<prefix_length>\d+)"
match_ip = re.findall(re_intf_ip, interface, flags=re.M)

for ip_info in match_ip:
val = {"prefix_length": int(ip_info[1])}
# v4_interfaces[intf_name][ip_info[0]] = val
Expand Down