Skip to content

Commit 44040bc

Browse files
committed
added is_mac_adddress() function to check if a string is a valid ethernet
mac address
1 parent 8344a2a commit 44040bc

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Random device bytes will be generated.
1919

2020
**list_vendors(_file_)** - return a python list [] with valid vendors
2121

22+
**is_mac_address(_mac_)** - Takes a string, and checks if it is a valid Ethernet
23+
MAC address. returns True or False(bool type)
24+
2225
Usage
2326
-----
2427

@@ -61,3 +64,9 @@ List valid vendor options as a list.
6164
g.list_vendors('/usr/share/wireshark/manuf')
6265
['Vendor1','Vendor2','etc']
6366
```
67+
68+
Check if a MAC address is valid
69+
```
70+
if g.is_mac_address('94:0C:98:BC:74:1C') == True:
71+
print('Valid Ethernet AAddress')
72+
```

generate_mac/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,20 @@ def list_vendors(vid_file):
190190
'''Returns a list[] of valid ETH Vendors that can be used with vid_file_vendor()'''
191191
file_lines = generate_mac._read_vid_file(vid_file)
192192
return list(generate_mac._valid_vendors)
193+
194+
def is_mac_address(mac_address):
195+
'''Test if a given string is a valid Ethernet MAC address. return True or False'''
196+
try:
197+
mac_bytes=mac_address.split(":")
198+
except:
199+
return False
200+
if len(mac_bytes) != 6:
201+
return False
202+
203+
# First Octet needs to be odd.
204+
mac_byte_bcast = mac_bytes[0][1]
205+
mac_byte_bcast = mac_byte_bcast.upper()
206+
if mac_byte_bcast in generate_mac._valid_bcast_char:
207+
return True
208+
else:
209+
return False

0 commit comments

Comments
 (0)