helper.py 886 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. import netifaces as netif
  3. import subprocess
  4. import sys
  5. def call(cmdnargs):
  6. try:
  7. output = subprocess.check_output(cmdnargs, stderr=subprocess.STDOUT)
  8. lines = output.splitlines()
  9. lines = [line.decode('utf-8') for line in lines]
  10. except subprocess.CalledProcessError as err:
  11. print(err)
  12. except:
  13. print(str(sys.exc_info()[0]))
  14. else:
  15. return lines
  16. return []
  17. def merge(a, b):
  18. if isinstance(a, dict) and isinstance(b, dict):
  19. d = dict(a)
  20. d.update({k: merge(a.get(k, None), b[k]) for k in b})
  21. return d
  22. if isinstance(a, list) and isinstance(b, list):
  23. return [merge(x, y) for x, y in itertools.izip_longest(a, b)]
  24. return a if b is None else b
  25. def getInterfaceMAC(interface):
  26. try:
  27. interface = netif.ifaddresses(interface)
  28. mac = interface[netif.AF_LINK]
  29. return mac[0]['addr']
  30. except:
  31. return None