Searches a command's output for a MAC address near a keyword. Each line of words in the output is case-insensitively searched for any of the given keywords. Upon a match, get_word_index is invoked to pick a word from the line, given the index of the match. For example, lambda i: 0
(command, args, keywords, get_word_index)
| 472 | |
| 473 | |
| 474 | def _find_mac_near_keyword(command, args, keywords, get_word_index): |
| 475 | """Searches a command's output for a MAC address near a keyword. |
| 476 | |
| 477 | Each line of words in the output is case-insensitively searched for |
| 478 | any of the given keywords. Upon a match, get_word_index is invoked |
| 479 | to pick a word from the line, given the index of the match. For |
| 480 | example, lambda i: 0 would get the first word on the line, while |
| 481 | lambda i: i - 1 would get the word preceding the keyword. |
| 482 | """ |
| 483 | stdout = _get_command_stdout(command, args) |
| 484 | if stdout is None: |
| 485 | return None |
| 486 | |
| 487 | first_local_mac = None |
| 488 | for line in stdout: |
| 489 | words = line.lower().rstrip().split() |
| 490 | for i in range(len(words)): |
| 491 | if words[i] in keywords: |
| 492 | try: |
| 493 | word = words[get_word_index(i)] |
| 494 | mac = int(word.replace(_MAC_DELIM, b''), 16) |
| 495 | except (ValueError, IndexError): |
| 496 | # Virtual interfaces, such as those provided by |
| 497 | # VPNs, do not have a colon-delimited MAC address |
| 498 | # as expected, but a 16-byte HWAddr separated by |
| 499 | # dashes. These should be ignored in favor of a |
| 500 | # real MAC address |
| 501 | pass |
| 502 | else: |
| 503 | if _is_universal(mac): |
| 504 | return mac |
| 505 | first_local_mac = first_local_mac or mac |
| 506 | return first_local_mac or None |
| 507 | |
| 508 | |
| 509 | def _parse_mac(word): |
no test coverage detected
searching dependent graphs…