|
5 | 5 |
|
6 | 6 | class generate_mac(): |
7 | 7 | '''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() |
11 | 11 | _vid_table = { |
12 | 12 | '36':2, |
13 | 13 | '32':3, |
@@ -138,24 +138,46 @@ def vid_file_random(vid_file): |
138 | 138 | output = vid_bytes + ":" + dev_bytes |
139 | 139 | return(output) |
140 | 140 |
|
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''' |
143 | 143 | file_lines = generate_mac._read_vid_file(vid_file) |
| 144 | + |
144 | 145 | line_vendor = "" |
145 | 146 | vid_bytes = "" |
146 | | - descrption = "" |
| 147 | + file_desc = "" # description from file |
147 | 148 | rand_line = "" |
| 149 | + # If the vendor is not in vendor list, throw an error, otherwise |
| 150 | + # function will hang |
148 | 151 | if vendor not in generate_mac._valid_vendors: |
149 | 152 | 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 |
159 | 181 |
|
160 | 182 | # Now generate the random device bytes |
161 | 183 | bytes_needed = rand_line[1] //2 |
|
0 commit comments