Skip to content

Instantly share code, notes, and snippets.

@tjstum
Created February 28, 2016 23:19
Show Gist options
  • Save tjstum/ac49e3e70dd302392564 to your computer and use it in GitHub Desktop.
Save tjstum/ac49e3e70dd302392564 to your computer and use it in GitHub Desktop.
getnode inspector
#!/usr/bin/env python2.7
"""
Inspect what each MAC address retrieval function would find
Useful for figuring out which subsystem is giving you the answer
"""
from __future__ import print_function
import uuid
import argparse
import sys
import traceback
def retrieve(method, anonymous):
print("Trying {}...".format(method.__name__))
try:
value = method()
except Exception:
print("{} failed!".format(method.__name__))
traceback.print_exc()
else:
if value is None:
print("{} returned None!".format(method.__name__))
return
to_print = anonymize_node(value) if anonymous else "0x{:X}".format(
value)
print("Result from {}:{}".format(method.__name__, to_print))
if (value >> 40) % 2:
print("That would fail")
anonymized_nodes = []
def anonymize_node(node):
if node not in anonymized_nodes:
anonymized_nodes.append(node)
return "0x{}".format(anonymized_nodes.index(node))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--anonymize", action="store_true",
help="Anonymize MAC addresses when printing them")
args = parser.parse_args()
if args.anonymize:
print("Addresses anonymized. Addresses will print out 0x### where ### "
"is an incrementing number. Identical addresses will have the "
"same number.")
if sys.platform == "win32":
getters = [uuid._windll_getnode, uuid._netbios_getnode,
uuid._ipconfig_getnode]
else:
getters = [uuid._unixdll_getnode, uuid._ifconfig_getnode,
uuid._arp_getnode, uuid._lanscan_getnode,
uuid._netstat_getnode]
for method in getters:
retrieve(method, args.anonymize)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment