utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. Utility functions.
  3. """
  4. import subprocess
  5. import re
  6. import os
  7. import logging
  8. logger = logging.getLogger("pypacker")
  9. def switch_wlan_channel(iface, channel, shutdown_prior=False):
  10. """
  11. Switch wlan channel to channel.
  12. Requirements: ifconfig, iwconfig
  13. iface -- interface name
  14. channel -- channel numer to be set as number
  15. shutdown_prior -- shut down interface prior to setting channel
  16. """
  17. if shutdown_prior:
  18. cmd_call = ["ifconfig", iface, "down"]
  19. subprocess.check_call(cmd_call)
  20. cmd_call = ["iwconfig", iface, "channel", "%d" % channel]
  21. subprocess.check_call(cmd_call)
  22. if shutdown_prior:
  23. cmd_call = ["ifconfig", iface, "up"]
  24. subprocess.check_call(cmd_call)
  25. def set_wlan_monmode(iface, monitor_active=True, reactivate=True):
  26. """
  27. Activate/deacivate monitor mode.
  28. Requirements: ifconfig, iwconfig
  29. monitor_active -- activate/deactivate monitor mode
  30. reactivate -- set interface to active at the end, else leave deactivated
  31. """
  32. cmd_call = ["ifconfig", iface, "down"]
  33. subprocess.check_call(cmd_call)
  34. mode = "monitor" if monitor_active else "managed"
  35. cmd_call = ["iwconfig", iface, "mode", mode]
  36. subprocess.check_call(cmd_call)
  37. try:
  38. cmd_call = ["iwconfig", iface, "retry", "0"]
  39. subprocess.check_call(cmd_call)
  40. # we don't need retry but this can improve performance
  41. except:
  42. # not implemented: don't care
  43. pass
  44. if reactivate:
  45. cmd_call = ["ifconfig", iface, "up"]
  46. subprocess.check_call(cmd_call)
  47. PROG_CHANNEL = re.compile(b"Channel ([\d]+) :")
  48. def get_available_wlan_channels(iface):
  49. """
  50. Requirements: iwlist
  51. return -- channels as integer list
  52. """
  53. cmd_call = ["iwlist", iface, "channel"]
  54. output = subprocess.check_output(cmd_call)
  55. # logger.debug("iwlist output: %r" % output)
  56. return [int(ch) for ch in PROG_CHANNEL.findall(output)]
  57. def set_ethernet_address(iface, ethernet_addr):
  58. """
  59. iface -- interface name
  60. ethernet_addr -- Ethernet address like "AA:BB:CC:DD:EE:FF"
  61. """
  62. cmd_call = ["ifconfig", iface, "down"]
  63. subprocess.check_call(cmd_call)
  64. cmd_call = ["ifconfig", iface, "hw", "ether", ethernet_addr]
  65. subprocess.check_call(cmd_call)
  66. cmd_call = ["ifconfig", iface, "up"]
  67. subprocess.check_call(cmd_call)
  68. MAC_VENDOR = {}
  69. PROG_MACVENDOR = re.compile(" ([\w\-]{8,8}) \(hex\)\t\t(.+)")
  70. def load_mac_vendor():
  71. """
  72. Load out.txt containing mac->vendor mappings into MAC_VENDOR
  73. See http://standards.ieee.org/develop/regauth/oui/oui.txt
  74. """
  75. logger.debug("loading oui file")
  76. current_dir = os.path.dirname(os.path.realpath(__file__))
  77. try:
  78. fh = open(current_dir + "/oui.txt", "r")
  79. for line in fh:
  80. hex_vendor = PROG_MACVENDOR.findall(line)
  81. if len(hex_vendor) > 0:
  82. # print(hex_vendor)
  83. MAC_VENDOR[hex_vendor[0][0].replace("-", ":")] = hex_vendor[0][1]
  84. fh.close()
  85. except Exception as e:
  86. logger.warning("could not load out.txt, is it present here? %s" % current_dir)
  87. def get_vendor_for_mac(mac):
  88. """
  89. mac -- First bytes of mac address as "AA:BB:CC" (uppercase!)
  90. return -- found vendor string or empty string
  91. """
  92. if len(MAC_VENDOR) == 0:
  93. load_mac_vendor()
  94. try:
  95. return MAC_VENDOR[mac]
  96. except KeyError:
  97. return ""
  98. # print(get_vendor_for_mac("00:00:00"))
  99. # print(get_vendor_for_mac("00:00:01"))
  100. # print(get_vendor_for_mac("00:10:00"))