-
Notifications
You must be signed in to change notification settings - Fork 2
/
mac.py
executable file
·197 lines (157 loc) · 7.13 KB
/
mac.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
"""
A utility class for MAC addresses.
MAC formats:
`none` : 1234567890AB
`ieee` : 12-34-56-78-90-AB
`onebyte`: 12:34:56:78:90:AB
`twobyte`: 1234:5678:90AB
"""
__author__ = "Thomas Howard"
__email__ = "thomas@cisco.com"
__license__ = "MIT - https://mit-license.org/"
import argparse
import re
import time
import random
import string
import sys
class MAC():
# Class variables
FORMATS = [
'none', # no separators: XXXXXXXXXXXX
'ieee', # onebyte + '-' separator: XX-XX-XX-XX-XX-XX
'onebyte', # onebyte + ':' separator (or any separator): XX:XX:XX:XX:XX:XX
'twobyte', # twobyte + ':' separator (or any separator): XXXX:XXXX:XXXX
]
@classmethod
def is_hex (self, s:str=None):
"""
Returns True if all characters in string s are hexadecimal digits, False otherwise.
- c (str): a single character string.
"""
try:
[int(c, 16) for c in s.split()]
return True
except ValueError:
return False
@classmethod
def normalize (self, mac:str=None):
"""
Returns a MAC address without any separators and all upppercase.
- mac (str): the MAC address in any format.
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
return mac.strip().translate(str.maketrans("", "", string.punctuation)).upper()
@classmethod
def is_mac (self, mac:str=None):
"""
Returns True if mac is a normalized, 12-digit MAC address, False otherwise.
- mac (str): a MAC address.
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
mac = self.normalize(mac)
return (len(mac) == 12 and self.is_hex(mac))
@classmethod
def is_random (self, mac:str=None):
"""
Returns True if mac is a random MAC address, False otherwise.
RADIUS:Calling-Station-ID MATCHES ^.[26AaEe].*
12:34:56:78:90:AB
0001 ──┘└── 0010
│├── 0: Unicast (evens)
│└── 1: Multicast (odds)
├── 0: globally unique [0,1,4,5,8,9,C,D]
└── 1: locally administered [2,6,A,E]
- mac (str): a MAC address.
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
mac = self.normalize(mac)
return (len(mac) == 12 and self.is_hex(mac))
@classmethod
def randomized (self, oui:str=None):
"""
Returns a randomized MAC address prefixed with the specified OUI or a randomized OUI if none is given.
- oui (str): an optional OUI.
"""
oui = '{:06X}'.format(random.randint(1, 16777216)) if oui == None else oui # 16777216 == 2^24
mac = oui+'{:06X}'.format(random.randint(1, 16777216)) # 'X' == capitalized hex
return self.to_format(mac)
# 💡 "format()" is a built-in Python function
@classmethod
def to_format (self, mac:str=None, format:str='ieee', sep:str='-'):
"""
Returns a formatted MAC address with the specified separator between groups of digits.
The default format is the IEEE802 format: <pre>xx-xx-xx-xx-xx-xx</pre>.
- mac (str): a MAC address.
- format (str): one of the supported formats (default: 'ieee'):
`none` : 1234567890AB
`ieee` : 12-34-56-78-90-AB
`onebyte`: 12:34:56:78:90:AB
`twobyte`: 1234:5678:90AB
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
if not isinstance(format, str): raise ValueError(f"format is not a string: {mac}")
if format not in self.FORMATS : raise ValueError(f"format is not one of [{','.join(self.FORMATS)}]: {format}")
if format == 'ieee': return self.fmt(mac, '-', 2)
if format == 'none': return self.normalize(mac)
if format == 'onebyte': return self.fmt(mac, sep, 2)
if format == 'twobyte': return self.fmt(mac, sep, 4)
# 💡 "format()" is a builtin Python function
@classmethod
def fmt (self, mac:str=None, sep:str='-', digits:int=2):
"""
Returns a formatted MAC address with the specified separator between groups of digits.
The default format is the IEEE802 format: <pre>xx-xx-xx-xx-xx-xx</pre>.
- mac (str): a MAC address.
- sep (int): a separator character. Default: '-'.
- digits (int): the number of digits in a group (0,2,4). O means no separator. Default: 2.
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
if not isinstance(sep, str): raise ValueError(f"separator is not a string: {sep}")
if not isinstance(digits, int): raise ValueError(f"digits is not an int: {digits}")
if digits not in [2,4]: raise ValueError(f"digits is not [2,4]: {digits}")
mac = self.normalize(mac)
groups = [] # onebyte or twobyte groups
for n in range(0, 12, digits): # range(start, stop[, step])
groups.append(mac[n:n+digits])
return sep.join(groups)
@classmethod
def get_oui (self, mac:str=None):
"""
Returns the organizationally unique identifier (OUI) (first 6 digits of the MAC address), regardless of delimiters.
- mac (str): a MAC address.
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
return self.normalize(mac)[0 : 6]
@classmethod
def get_nic (self, mac:str=None):
"""
Returns the network interface card (NIC) portion (last 6 digits) of the MAC address, regardless of delimiters.
- mac (str): a MAC address.
"""
if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
return self.normalize(mac)[6:]
# @classmethod
# def is_random (self, mac:str=None):
# """
# Returns True if the MAC address *mac* is random, False otherwise.
# - mac (str): a MAC address.
# """
# if not isinstance(mac, str): raise ValueError(f"mac is not a string: {mac}")
# pass
if __name__ == "__main__":
argp = argparse.ArgumentParser()
argp.description = __doc__
argp.add_argument('-o', '--oui', help='the base OUI', required=False)
argp.add_argument('-n', '--number', default=1, help='the number of MACs to create. Default is 1', required=False)
argp.add_argument('-f', '--format', choices=['ieee', 'none', 'onebyte', 'twobyte'], default='ieee', help='the address format', required=False)
argp.add_argument('-s', '--sep', default='-', help='the separator character', required=False)
args = argp.parse_args()
# Use the specified OUI otherwise generate a random one
print(f"{random.randint(1, 16777216)}")
oui = args.oui.strip().upper() if args.oui else '{:06X}'.format(random.randint(1, 16777216))
number = int(args.number) if args.number else 1
for n in range(0, number):
mac = oui+'{:06X}'.format(random.randrange(1, 16777216))
print(to_format(mac, args.format, args.sep))