diff --git a/start_hotpots.sh b/start_hotpots.sh new file mode 100644 index 0000000..0d72750 --- /dev/null +++ b/start_hotpots.sh @@ -0,0 +1 @@ +echo 'orangepi' | sudo -S nohup python3 /home/orangepi/wifi_hotpot/wifi_manager.py > /home/orangepi/wifi_hotpot/log.log 2>&1 & \ No newline at end of file diff --git a/wifi_manager.py b/wifi_manager.py index 04a8133..d03027e 100644 --- a/wifi_manager.py +++ b/wifi_manager.py @@ -9,23 +9,22 @@ import subprocess import re import os import time +import datetime from flask import Flask, render_template, request, redirect, url_for, make_response app = Flask(__name__) # 检测 Wi-Fi 连接状态 def check_wifi_connection(): - try: - # 使用nmcli命令检查Wi-Fi连接状态 - output = subprocess.check_output(['nmcli', 'device', 'status']).decode('utf-8') - - # 分析输出以查找Wi-Fi连接状态 - for line in output.split('\n'): - if 'wifi' in line and 'connected' in line: - return True - except subprocess.CalledProcessError as e: - print(f"Error checking Wi-Fi status: {e}") - + cmd = "nmcli dev status" + result = subprocess.run(cmd, shell=True, capture_output=True, text=True) + output = result.stdout.strip() + lines = output.split("\n")[1:] # Skip the header line + for line in lines: + columns = line.split() + print(columns) + if len(columns) >= 4 and columns[2] == "connected": + return True return False def restart_hotspot(): @@ -41,7 +40,7 @@ def restart_hotspot(): subprocess.run(['sudo', 'nmcli', 'radio', 'wifi', 'on'], check=True) subprocess.Popen('sudo nohup create_ap wlan0 eth0 Takway-Toys --no-virt > create_ap.log 2>&1 &', shell=True) except subprocess.CalledProcessError as e: - print(f"Error setting up hotspot: {e}") + print(f"{datetime.datetime.now()} Error stopping create_ap hotspot service: {e}") def scan_wifi(): @@ -100,9 +99,9 @@ def connect_wifi(ssid, password): # 关闭热点 try: subprocess.run(['sudo', 'create_ap', '--stop', 'wlan0'], check=True) - print("Stopping create_ap service") + print(f"{datetime.datetime.now()} Stopping create_ap service") except subprocess.CalledProcessError as e: - print(f"Error stopping hotspot: {e}") + print(f"{datetime.datetime.now()} Error stopping hotspot: {e}") time.sleep(1) @@ -114,10 +113,10 @@ def connect_wifi(ssid, password): # 连接到用户选择的 Wi-Fi 网络 try: subprocess.run(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password], check=True) - print(f"Successfully connected to Wi-Fi: {ssid}") + print(f"{datetime.datetime.now()} Successfully connected to Wi-Fi: {ssid}") return True except subprocess.CalledProcessError as e: - print(f"Error connecting to Wi-Fi: {e}") + print(f"{datetime.datetime.now()} Error connecting to Wi-Fi: {e}") return False # 主页 @@ -137,7 +136,7 @@ def index(): def submit(): ssid = request.form['ssid'] password = request.form['password'] - print(f"Connecting to Wi-Fi: {ssid} with password {password}") + print(f"{datetime.datetime.now()} Connecting to Wi-Fi: {ssid} with password {password}") connected = connect_wifi(ssid, password) time.sleep(5) @@ -155,11 +154,12 @@ if __name__ == '__main__': if not debug_mode: if check_wifi_connection(): - print("已连接到 Wi-Fi 网络") + print(f"{datetime.datetime.now()} 已连接到 Wi-Fi 网络") else: - print("未连接到 Wi-Fi 网络") + print(f"{datetime.datetime.now()} 未连接到 Wi-Fi 网络") + exit(0) - print("Starting Flask server") + print(f"{datetime.datetime.now()} Starting Flask server") print("----------------------------") wifi_list = scan_wifi() print("----------------------------")