Skip to content

Commit 1050c0f

Browse files
committed
vid_file_vendor() now has an optional description search within a vendor
1 parent 7f1f28d commit 1050c0f

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Supported functions:
1111

1212
**vid_file_random(_file_)** - uses random line from wireshark's manuf file
1313

14-
**vid_file_vendor(_file, name_)** - specify a vendor name, uses wireshark's manuf file
15-
instead of being completely random
14+
**vid_file_vendor(_file, vendor name, desc=optional_)** - specify a vendor name,
15+
uses wireshark's manuf file instead of being completely random. May optionally
16+
specify desc, which searches description within the vendor field
1617

1718
**vid_provided(_vid bytes_)** - specify the VID bytes when calling the function.
1819
Random device bytes will be generated.
@@ -51,6 +52,12 @@ vendor, by name.
5152
generate_mac.vid_file_vendor('/usr/share/wireshark/manuf', '3Com')
5253
'00:06:8C:C7:3F:93'
5354
```
55+
*OPTIONAL:* this can also now search the description field
56+
57+
```
58+
generate_mac.vid_file_vendor("/usr/share/wireshark/manuf","Motorola","Wuhan")
59+
'40:A1:08:16:11:D2'
60+
```
5461

5562
Provide the vendor bytes in a string. Generate Host bytes only
5663
```

generate_mac/__init__.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
class generate_mac():
77
'''MAC address generation class'''
8-
_valid_char = "0123456789ABCDEF"
9-
_valid_bcast_char = "02468ACE"
10-
_valid_vendors = set()
8+
_valid_char = "0123456789ABCDEF"
9+
_valid_bcast_char = "02468ACE"
10+
_valid_vendors = set()
1111
_vid_table = {
1212
'36':2,
1313
'32':3,
@@ -138,24 +138,46 @@ def vid_file_random(vid_file):
138138
output = vid_bytes + ":" + dev_bytes
139139
return(output)
140140

141-
def vid_file_vendor(vid_file,vendor):
142-
'''Generates a random MAC from a specified vendor name'''
141+
def vid_file_vendor(vid_file,vendor,desc=None):
142+
'''Generates a random MAC from a specified vendor name. Takes three parameters. file name, vendor name, and description which is optional. description can be a partial match'''
143143
file_lines = generate_mac._read_vid_file(vid_file)
144+
144145
line_vendor = ""
145146
vid_bytes = ""
146-
descrption = ""
147+
file_desc = "" # description from file
147148
rand_line = ""
149+
# If the vendor is not in vendor list, throw an error, otherwise
150+
# function will hang
148151
if vendor not in generate_mac._valid_vendors:
149152
raise KeyError(vendor + " has no associated VID byte in manuf file")
150-
while line_vendor != vendor:
151-
# Get a random line from the file, and then proccess
152-
rand_line = file_lines[ random.randrange( len(file_lines) ) ]
153-
rand_line = generate_mac._get_processed_vid(rand_line)
154-
# Fill in the blanks
155-
vid_bytes = rand_line[0]
156-
bytes_needed = rand_line[1]
157-
line_vendor = rand_line[2]
158-
description = rand_line[3]
153+
if desc == None:
154+
while line_vendor != vendor:
155+
# Get a random line from the file, and then proccess
156+
rand_line = file_lines[ random.randrange( len(file_lines) ) ]
157+
rand_line = generate_mac._get_processed_vid(rand_line)
158+
# Fill in the blanks
159+
if len(rand_line) == 4:
160+
vid_bytes,bytes_needed,line_vendor,file_desc = rand_line
161+
elif len(rand_line) == 3:
162+
vid_bytes,bytes_needed,line_vendor = rand_line
163+
164+
else:
165+
search_lines = []
166+
for line in file_lines:
167+
line = generate_mac._get_processed_vid(line)
168+
if len(line) == 4:
169+
vid_bytes,bytes_needed,line_vendor,file_desc = line
170+
else:
171+
continue
172+
173+
if line_vendor.lower() == vendor.lower() and desc.lower() in file_desc.lower():
174+
search_lines.append(line)
175+
176+
if len(search_lines) == 0:
177+
raise KeyError("No such description with " + vendor + " in file")
178+
179+
rand_line = search_lines[ random.randrange( len(search_lines) ) ]
180+
vid_bytes,bytes_needed,line_vendor,file_desc = rand_line
159181

160182
# Now generate the random device bytes
161183
bytes_needed = rand_line[1] //2

0 commit comments

Comments
 (0)