-
Notifications
You must be signed in to change notification settings - Fork 0
/
disc_functs.py
96 lines (80 loc) · 2.73 KB
/
disc_functs.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def create_filename(sub_dir1: str, extension: str = "", sub_dir2="") -> str:
"""
returns a valid path regardless of the OS
Args:
sub_dir1 (str): name of the sub directory off the cwd required
extension (str): string appended after hostname - ex. -interface.txt
sub_dir2 (str, optional): if a nested sub_dir is used Defaults to "".
Returns:
str: full pathname of the file to be written
"""
current_path = os.getcwd()
extension = hostname + extension
int_report = os.path.join(current_path, sub_dir1, sub_dir2, extension)
return int_report
def remove_empty_lines(filename: str) -> str:
"""
Removes empty lines from the file
Args:
filename (str): file in the cwd to be opened
Returns:
Nothing - updated file is written to disk
"""
if not os.path.isfile(filename):
print("{} does not exist ".format(filename))
return
with open(filename) as filehandle:
lines = filehandle.readlines()
with open(filename, "w") as filehandle:
lines = filter(lambda x: x.strip(), lines)
filehandle.writelines(lines)
def format_mac(mac: str) -> str:
"""
Converts most common MAC address formats into all formats
008041aefd7e - valid\n
00-80-41-ae-fd-7e - valid\n
00:80:41:AE:FD:7E - valid\n
'0080.41ae.fd7e', - valid\n
00:80:41:ae:fd:7e - valid\n
'00:80:41:ae:fd:7e\n\t', - valid
Args:
mac (str): A valid mac address format
Returns:
str: MAC_Types in aa:bb:cc:dd:ee:ff format
"""
mac = re.sub("[.:-]", "", mac).lower() # remove delimiters, convert to lc
mac = "".join(mac.split()) # remove whitespaces
assert len(mac) == 12 # length should be now exactly 12 (eg. 008041aefd7e)
assert mac.isalnum() # should only contain letters and numbers
colon = mac
hpe = mac
cisco = mac
ms = mac
# convert mac in canonical form (eg. 00:80:41:ae:fd:7e)
colon = ":".join(["%s" % (mac[i : i + 2]) for i in range(0, 12, 2)])
# HPE format
hpe = "-".join(["%s" % (mac[i : i + 6]) for i in range(0, 12, 6)])
# Cisco format
cisco = ".".join(["%s" % (mac[i : i + 4]) for i in range(0, 12, 4)])
# MS format
ms = "-".join(["%s" % (mac[i : i + 2]) for i in range(0, 12, 2)])
# Build list of macs
list1 = [colon, hpe, cisco, ms, mac]
MAC_Types = "\n".join(list1)
return MAC_Types
def compareList(l1, l2):
l1.sort()
l2.sort()
if l1 == l2:
return "Equal"
else:
return "Non equal"
l1 = [1, 2, 3]
l2 = [2, 1, 3]
print("First comparison", compareList(l1, l2))
l3 = [1, 2, 3]
l4 = [1, 2, 4]
print("Second comparison", compareList(l3, l4))
# Output
# First comparison Equal
# Second comparison Non equal