diff --git a/README.md b/README.md index 99e7c2e..fd8bd0f 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Description=Hotspot Service After=network.target [Service] -ExecPre=/bin/sleep 5 +ExecPre=/bin/sleep 2 ExecStart=create_ap wlan0 eth0 Takway-Toys --no-virt User=root @@ -117,7 +117,7 @@ Wants=network.target [Service] Type=oneshot -ExecStart=python3 /home/orangepi/wifi_hotpot/wifi_manager.py +ExecStart=/bin/bash /home/orangepi/wifi_hotpot/start_hotpots.sh WorkingDirectory=/home/orangepi/wifi_hotpot User=root @@ -136,6 +136,7 @@ sudo systemctl enable wifi_hotspot.service ``` sudo systemctl start wifi_hotspot.service +# sudo systemctl stop wifi_hotspot.service ``` - 查看输出 diff --git a/start_hotpots.sh b/start_hotpots.sh index 39ede04..c989b27 100644 --- a/start_hotpots.sh +++ b/start_hotpots.sh @@ -1,4 +1,4 @@ # run this script to start the hotpot server python3 wifi_manager.py # cd to the TakwayBoard directory and start the client -cd ../TakwayBoard && python3 ws_client.py \ No newline at end of file +cd ../TakwayBoard && nohup python3 ws_client.py > output.log 2>&1 & \ No newline at end of file diff --git a/wifi_manager.py b/wifi_manager.py index f9ce405..89b9228 100644 --- a/wifi_manager.py +++ b/wifi_manager.py @@ -32,6 +32,27 @@ def network_error_light(): break close_app() +def edit_line_in_file(file_path, search_string, replacement_string): + try: + # 读取文件内容 + with open(file_path, 'r') as file: + lines = file.readlines() + + # 替换包含特定内容的行 + with open(file_path, 'r+') as file: + file.seek(0) # 移动到文件开头 + file.truncate() # 清空文件内容 + for line in lines: + if search_string in line: + file.write(replacement_string + '\n') + else: + file.write(line) + + print("The line has been edited.") + except FileNotFoundError: + print(f"The file {file_path} does not exist.") + except IOError as e: + print(f"An error occurred: {e}") app = Flask(__name__) @@ -93,6 +114,11 @@ def check_wifi_connection(): orangepi.set_led_off('blue') return True, wifi_ssid elif len(columns) >= 4 and columns[0] == "wlan0" and columns[2] == "unmanaged": + # 使用函数 + file_path = '/etc/NetworkManager/NetworkManager.conf' + search_string = 'unmanaged-devices=interface-name:' + replacement_string = '' # 如果不需要替换为其他内容,可以设置为空字符串 + edit_line_in_file(file_path, search_string, replacement_string) ''' # 从 /etc/NetworkManager/NetworkManager.conf 文件中移除或注释掉 [keyfile] 部分的 unmanaged-devices 条目,因为它可能阻止了 NetworkManager 管理 wlan0 接口。编辑文件如下: ``` @@ -101,7 +127,6 @@ def check_wifi_connection(): ``` sudo systemctl restart NetworkManager ''' - print() network_error_light() return False, None