forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_util.py
More file actions
41 lines (31 loc) · 1.2 KB
/
ip_util.py
File metadata and controls
41 lines (31 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
import re
import logging
logger = logging.getLogger(__name__)
def sum_ifaces_counts(counter_out, ifaces, column):
if len(ifaces) == 0:
return 0
if len(ifaces) == 1:
return int(counter_out[ifaces[0]][column].replace(",", ""))
return sum([int(counter_out[iface][column].replace(",", "")) for iface in ifaces])
def parse_interfaces(output_lines, pc_ports_map):
"""
Parse the interfaces from 'show ip route' into an array
"""
route_targets = []
ifaces = []
output_lines = output_lines[3:]
for item in output_lines:
match = re.search(r"(Ethernet\d+|PortChannel\d+)", item)
if match:
route_targets.append(match.group(0))
for route_target in route_targets:
if route_target.startswith("Ethernet"):
ifaces.append(route_target)
elif route_target.startswith("PortChannel") and route_target in pc_ports_map:
ifaces.extend(pc_ports_map[route_target])
return route_targets, ifaces
def random_mac():
return "02:00:00:%02x:%02x:%02x" % (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))