You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ap.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #! /usr/bin/python3
  2. import argparse
  3. import re
  4. import tempfile
  5. import os
  6. CONFIG_FILE_PATH = "/etc/create_ap/default.conf"
  7. def read_config():
  8. with open(CONFIG_FILE_PATH, "r") as f:
  9. return f.read()
  10. def write_config(config):
  11. with tempfile.NamedTemporaryFile(delete=False) as f:
  12. f.write(config.encode('utf-8'))
  13. return f.name
  14. def replace_config_item(config, item, value):
  15. return re.sub("(%s)=.*" % item, "\\1=%s" % value, config)
  16. def replace_config_items(config, values):
  17. for item in values:
  18. config = replace_config_item(config, item, values[item])
  19. return config
  20. def which(program):
  21. import os
  22. def is_exe(fpath):
  23. return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
  24. fpath, fname = os.path.split(program)
  25. if fpath:
  26. if is_exe(program):
  27. return program
  28. else:
  29. for path in os.environ["PATH"].split(os.pathsep):
  30. path = path.strip('"')
  31. exe_file = os.path.join(path, program)
  32. if is_exe(exe_file):
  33. return exe_file
  34. return None
  35. def main():
  36. parser = argparse.ArgumentParser(description='Create Wi-Fi access point')
  37. parser.add_argument('method', help='The sharing method (bridge, nat, none)')
  38. parser.add_argument('wlan_iface', help='The wireless device to use (wlanX)')
  39. parser.add_argument('net_iface', help='The interface with network access (ethX)', default='', nargs='?')
  40. args = parser.parse_args()
  41. values = {
  42. "WIFI_IFACE": args.wlan_iface,
  43. "INTERNET_IFACE": args.net_iface,
  44. "SHARE_METHOD": args.method
  45. }
  46. config = read_config()
  47. config = replace_config_items(config, values)
  48. temp_file_path = write_config(config)
  49. print("Temp config file: %s" % temp_file_path)
  50. create_ap_path = which('create_ap')
  51. os.execv(create_ap_path, [create_ap_path, '--config', temp_file_path])
  52. main()