瀏覽代碼

add service restart

Tobias Simetsreiter 4 年之前
父節點
當前提交
c2112cd0dd

+ 0 - 23
src/bootstrap_captive/api/__init__.py

@@ -59,29 +59,6 @@ def submit_form(inp):
             "trace": traceback.format_exc(),
         }
 
-def get_data(inp):
-    wifi_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
-    ssid = "";
-    pw = ""
-    try:
-        import re
-        with open(wifi_file) as fd:
-            wifi_config = fd.read()
-
-        reg = re.search(r'ssid="(.*)', wifi_config)
-        if reg:
-            ssid = reg.groups()[0].rstrip().rstrip('"')
-        reg = re.search(r'psk="(.*)', wifi_config)
-        if reg:
-            pw = reg.groups()[0].rstrip().rstrip('"')
-    except Exception as ex:
-        print(ex)
-    return {
-        "ok":True,
-        "ssid": ssid,
-        "pw": pw,
-    }
-
 def disable_portal(inp):
     from subprocess import Popen,PIPE,DEVNULL
     import signal

+ 1 - 0
src/bootstrap_captive/form/services.json

@@ -23,6 +23,7 @@
                 "status",
                 "enable",
                 "disable",
+                "restart",
                 "start",
                 "stop"
             ]

+ 19 - 3
src/bootstrap_captive/form/wifi.py

@@ -1,5 +1,24 @@
+wifi_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
 
 def get_form(form):
+    try:
+        import re
+        with open(wifi_file) as fd:
+            wifi_config = fd.read()
+
+        reg = re.search(r'ssid="(.*)', wifi_config)
+        if reg:
+            ssid = reg.groups()[0].rstrip().rstrip('"')
+        reg = re.search(r'psk="(.*)', wifi_config)
+        if reg:
+            pw = reg.groups()[0].rstrip().rstrip('"')
+        form["value"] = {
+            "ssid": ssid,
+            "password": pw,
+        }
+    except Exception as ex:
+        print(ex)
+
     return form
 
 def submit_form(inp, form):
@@ -10,7 +29,6 @@ def submit_form(inp, form):
     inp_conf = inp["wifi_config"]
     ssid = inp_conf["ssid"].replace('"','\"')
     pw = inp_conf["password"].replace('"','\"')
-    wifi_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
     with open(wifi_file) as fd:
         wifi_conf = fd.read()
 
@@ -24,5 +42,3 @@ def submit_form(inp, form):
 
     return resp
 
-def get_data(form):
-    pass

+ 4 - 2
src/bootstrap_captive/install.py

@@ -33,16 +33,18 @@ set -x
 dpkg -l hostapd dnsmasq python3-rpi.gpio 2>&1 1> /dev/null || dpkg -i -R blob/
 systemctl unmask hostapd.service
 systemctl disable hostapd.service dnsmasq.service
+systemctl disable bootstrap_captive@server.service || true
 sed -i 's/^source-directory.*$/source \/etc\/network\/interfaces.d\/*/' /etc/network/interfaces
 touch /etc/network/interfaces.d/wlan0.conf
 rm -f /etc/hostapd/hostapd.conf
 ln -s "{0}/hostapd.conf" /etc/hostapd/hostapd.conf
 mv /etc/dnsmasq.conf /etc/dnsmasq.orig || true
 ln -s "{0}/dnsmasq.conf" /etc/dnsmasq.conf
+rm -f /lib/systemd/system/bootstrap_captive_server.service
 rm -f /lib/systemd/system/bootstrap_captive@.service
-ln -s "{0}/bootstrap_captive@.service" /lib/systemd/system/
+ln -s "{0}/bootstrap_captive_server.service" /lib/systemd/system/
 systemctl daemon-reload
-systemctl disable bootstrap_captive@server.service
+systemctl disable bootstrap_captive_server.service
 
 """.format(args.install_dir)
     from subprocess import run

+ 2 - 2
src/bootstrap_captive@.service → src/bootstrap_captive_server.service

@@ -1,5 +1,5 @@
 [Unit]
-Description=A Captive Portal for Raspberry Setup. Argument: %I
+Description=A Captive Portal for Raspberry Setup
 After=network.target
 StartLimitIntervalSec=0
 
@@ -8,7 +8,7 @@ Type=simple
 Restart=always
 RestartSec=1
 User=root
-ExecStart=/usr/local/bin/bootstrap_captive %i 
+ExecStart=/usr/local/bin/bootstrap_captive server
 
 [Install]
 WantedBy=multi-user.target

+ 2 - 2
src/main.py

@@ -67,7 +67,7 @@ sleep 3
 systemctl start hostapd.service
 while ! systemctl is-active dnsmasq.service;
 do
-    systemctl start networking dnsmasq.service bootstrap_captive@server.service dhcpcd;
+    systemctl start networking dnsmasq.service bootstrap_captive_server.service dhcpcd;
     sleep 1
 done
 """
@@ -86,7 +86,7 @@ systemctl stop networking hostapd.service dnsmasq.service dhcpcd.service wpa_sup
 ip addr flush wlan0
 sleep 5
 systemctl restart networking dhcpcd.service wpa_supplicant.service;
-systemctl stop bootstrap_captive@server.service
+systemctl stop bootstrap_captive_server.service
 """
     with open("/var/log/bootstrap_captive.log", "a") as fd:
         run(COM, shell=True, stdout=fd, stderr=fd)

+ 3 - 2
src/wpa_supplicant.conf

@@ -3,6 +3,7 @@ update_config=1
 country=DE
 
 network={
- ssid=""
- psk=""
+ ssid="<none>"
+ scan_ssid=1
+ psk="<none>"
 }

+ 4 - 4
wpa_supplicant.conf

@@ -1,8 +1,8 @@
+country=DE
 ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
 update_config=1
-
 network={
-    ssid="example"
-    scan_ssid=1
-    password="foobar"
+	ssid="MyWiFiNetwork"
+	psk="aVeryStrongPassword"
+	key_mgmt=WPA-PSK
 }