Skip to content

Commit aaa8054

Browse files
committed
fixed is_mac_address() to complete checks for validity
1 parent 1050c0f commit aaa8054

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

generate_mac/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,21 @@ def _gen_rand_bytes(bytes_needed):
9999
def _is_byte(test_byte):
100100
'''Tests if a string is a hexdecimal byte, returns True/False'''
101101
test_byte = test_byte.strip(":")
102+
103+
# Bytes need to be two hexdecimal chars
104+
if len(test_byte) != 2:
105+
return False
106+
102107
# If a byte does not convert from hex, its not a byte
103108
try:
104109
test_byte = int(test_byte,16)
105110
except:
106111
return False
112+
107113
# If the value is out of range for a single byte, it is not
108114
if 0 < test_byte > 255:
109115
return False
116+
110117
#If we get to the end, its a byte
111118
return True
112119

@@ -214,13 +221,19 @@ def list_vendors(vid_file):
214221

215222
def is_mac_address(mac_address):
216223
'''Test if a given string is a valid Ethernet MAC address. return True or False'''
224+
# Check to make sure its 6 fields split with a colon - :
217225
try:
218226
mac_bytes=mac_address.split(":")
219227
except:
220228
return False
221229
if len(mac_bytes) != 6:
222230
return False
223231

232+
# Make sure all bytes are in fact bytes
233+
for byte in mac_bytes:
234+
if generate_mac._is_byte(byte) == False:
235+
return False
236+
224237
# First Octet needs to be odd.
225238
mac_byte_bcast = mac_bytes[0][1]
226239
mac_byte_bcast = mac_byte_bcast.upper()
262 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)