Browse Source

init

master
Robin Thoni 7 years ago
commit
9c10a14896
1 changed files with 69 additions and 0 deletions
  1. 69
    0
      ap.py

+ 69
- 0
ap.py View File

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

Loading…
Cancel
Save