first commit
This commit is contained in:
commit
c515650f33
|
@ -0,0 +1,136 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Wi-Fi 设置</title>
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
|
||||
</head>
|
||||
<body>
|
||||
<div class="pageTitle">配置</div>
|
||||
<div class="inputLabel">WIFI 名称</div>
|
||||
<div class="inputDiv">
|
||||
<input id="wifiName" type="text" placeholder="请选择WiFi">
|
||||
</div>
|
||||
|
||||
<div class="inputLabel">WIFI 密码</div>
|
||||
<div class="inputDiv">
|
||||
<input id="wifiPassword" type="password" placeholder="请输入密码">
|
||||
</div>
|
||||
<div class="btnDiv" style="margin-top: 14vh;">
|
||||
<button class="btnSave" onclick="submitForm()">保存</button>
|
||||
</div>
|
||||
<div class="btnDiv" style="margin-top: 2vh;">
|
||||
<button class="btnRefresh">刷新</button>
|
||||
</div>
|
||||
|
||||
<div class="powerDiv">
|
||||
<span style="margin-right: 2%">Powered by</span>
|
||||
<span style="color: #3498ff;">Takway.ai</span>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function submitForm() {
|
||||
const ssid = document.getElementById('wifiName').value;
|
||||
const password = document.getElementById('wifiPassword').value;
|
||||
|
||||
fetch('/submit', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: `ssid=${encodeURIComponent(ssid)}&password=${encodeURIComponent(password)}`
|
||||
})
|
||||
.then(response => response.redirected ? window.location.href = response.url : null)
|
||||
.catch(error => console.error('Error:', error));
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
<style>
|
||||
*{
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
body{
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
.pageTitle{
|
||||
display: grid;
|
||||
place-items: center;
|
||||
height: 12vh;
|
||||
font-size: 3.5vh;
|
||||
font-weight: bold;
|
||||
margin-bottom: 2vh;
|
||||
}
|
||||
.inputLabel{
|
||||
margin-left: 7.5%;
|
||||
margin-bottom: 2vh;
|
||||
font-size: 2.5vh;
|
||||
}
|
||||
.inputDiv{
|
||||
width: 100%;
|
||||
display:flex;
|
||||
justify-content: center;
|
||||
}
|
||||
input[type=text]{
|
||||
width: 85%;
|
||||
margin-bottom: 2vh;
|
||||
padding: 1.3vh 1.3vh 1.3vh 2.2vh;
|
||||
font-size: 1.8vh;
|
||||
background-color: #f5f5f7;
|
||||
border: 0px;
|
||||
border-radius:5vh;
|
||||
}
|
||||
input[type=password]{
|
||||
width: 85%;
|
||||
margin-bottom: 2vh;
|
||||
padding: 1.3vh 1.3vh 1.3vh 2.2vh;
|
||||
font-size: 1.8vh;
|
||||
background-color: #f5f5f7;
|
||||
border: 0px;
|
||||
border-radius:5vh;
|
||||
}
|
||||
input[type=checkbox]{
|
||||
transform: scale(3);
|
||||
margin-left: 8%;
|
||||
}
|
||||
input::placeholder{
|
||||
color: #a9a9aa;
|
||||
}
|
||||
.pwShow{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
font-size: 2.5vh;
|
||||
}
|
||||
.btnDiv{
|
||||
width: 100%;
|
||||
display:flex;
|
||||
justify-content: center;
|
||||
}
|
||||
.btnSave{
|
||||
font-size: 3vh;
|
||||
color: white;
|
||||
background-color: #3498ff;
|
||||
border: 0px;
|
||||
width: 90%;
|
||||
border-radius:5vh;
|
||||
padding: 1.5vh;;
|
||||
}
|
||||
.btnRefresh{
|
||||
font-size: 3vh;
|
||||
color: #389aff;
|
||||
background-color: #cce6ff;
|
||||
border: 0px;
|
||||
width: 90%;
|
||||
border-radius:5vh;
|
||||
padding: 1.5vh;
|
||||
}
|
||||
.powerDiv{
|
||||
margin-top: 25vh;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
font-size: 2vh;
|
||||
}
|
||||
</style>
|
||||
</html>
|
|
@ -0,0 +1,164 @@
|
|||
import subprocess
|
||||
import os
|
||||
import time
|
||||
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}")
|
||||
|
||||
return False
|
||||
|
||||
# 设置热点
|
||||
def set_hotspot():
|
||||
try:
|
||||
subprocess.run(['sudo', 'create_ap', 'wlan0', 'eth0', 'Takway-Toy', '--no-virt'], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error setting up hotspot: {e}")
|
||||
|
||||
def scan_wifi():
|
||||
try:
|
||||
subprocess.run(['nmcli', 'dev', 'wifi', 'rescan'], check=True)
|
||||
time.sleep(1)
|
||||
output = subprocess.check_output(['nmcli', 'dev', 'wifi'])
|
||||
wifi_list = []
|
||||
lines = output.decode().splitlines()
|
||||
print(lines)
|
||||
print(len(lines))
|
||||
# for new in lines.split("', '"):
|
||||
# print(f"new: {new}")
|
||||
print("###### ")
|
||||
for idx, line in enumerate(lines[1:]):
|
||||
print(f"{idx}: {line}")
|
||||
|
||||
# 分割字符串以空格为分隔符
|
||||
columns = line.split()
|
||||
|
||||
# 提取第一列和第三列的值
|
||||
mac_address = columns[0]
|
||||
|
||||
ssid = " ".join(columns[1:3])
|
||||
if "Infra" in ssid:
|
||||
ssid = ssid.replace(" Infra", "")
|
||||
print("ssid: ", ssid.strip())
|
||||
|
||||
# 提取第六列的强度值
|
||||
last_info = columns[4:8]
|
||||
# 去除所有的空格
|
||||
columns = line.replace(" ", "")
|
||||
# 提取"Mbit/s"后两位数字
|
||||
strength = int(columns[columns.index("Mbit/s")+6:columns.index("Mbit/s")+8])
|
||||
|
||||
|
||||
print("MAC地址:", mac_address)
|
||||
print("SSID:", ssid)
|
||||
print("强度:", strength)
|
||||
|
||||
wifi_list.append({'ssid': ssid, 'signal': strength})
|
||||
if len(wifi_list) == 15:
|
||||
break
|
||||
|
||||
return wifi_list
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error scanning for Wi-Fi networks: {e}")
|
||||
return []
|
||||
|
||||
def scan_wifi_table():
|
||||
import csv
|
||||
import subprocess
|
||||
from io import StringIO
|
||||
|
||||
# 调用 nmcli dev wifi 命令并获取输出
|
||||
output = subprocess.check_output(['nmcli', 'dev', 'wifi']).decode()
|
||||
|
||||
# 创建一个内存中的文件对象
|
||||
file_object = StringIO(output)
|
||||
|
||||
# 使用csv模块读取文件对象,并指定分隔符为whitespace
|
||||
reader = csv.reader(file_object, delimiter=' ')
|
||||
|
||||
# 将数据存储到二维列表中
|
||||
data = [row for row in reader]
|
||||
|
||||
# 打印表格数据
|
||||
for row in data:
|
||||
print(row)
|
||||
|
||||
# 连接 Wi-Fi
|
||||
def connect_wifi(ssid, password):
|
||||
scan_wifi()
|
||||
time.sleep(1)
|
||||
try:
|
||||
subprocess.run(['sudo', 'create_ap', '--stop', 'wlan0'], check=True)
|
||||
print("Stopping create_ap service")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error stopping hotspot: {e}")
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
try:
|
||||
subprocess.run(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error connecting to Wi-Fi: {e}")
|
||||
|
||||
time.sleep(5)
|
||||
# 检查是否成功连接Wi-Fi
|
||||
if check_wifi_connection():
|
||||
print("成功连接到Wi-Fi,程序即将退出。")
|
||||
os._exit(0) # 成功连接后退出程序
|
||||
else:
|
||||
print("Wi-Fi连接失败。")
|
||||
|
||||
# 主页
|
||||
@app.route('/')
|
||||
def index():
|
||||
wifi_list = scan_wifi()
|
||||
response = make_response(render_template('index.html', wifi_list=wifi_list))
|
||||
response.headers.set('Content-Type', 'text/html')
|
||||
response.headers.set('Apple-Web-App-Capable', 'yes')
|
||||
response.headers.set('Apple-Mobile-Web-App-Status-Bar-Style', 'black-translucent')
|
||||
return response
|
||||
|
||||
# 提交 Wi-Fi 信息
|
||||
@app.route('/submit', methods=['POST'])
|
||||
def submit():
|
||||
ssid = request.form['ssid']
|
||||
password = request.form['password']
|
||||
print(f"Connecting to Wi-Fi: {ssid} with password {password}")
|
||||
connect_wifi(ssid, password)
|
||||
stop_hotspot() # 关闭热点
|
||||
return redirect(url_for('index'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
debug_mode = True # 设置为 True 以跳过 Wi-Fi 连接状态检测
|
||||
|
||||
if not debug_mode:
|
||||
if check_wifi_connection():
|
||||
print("已连接到 Wi-Fi 网络")
|
||||
else:
|
||||
print("未连接到 Wi-Fi 网络")
|
||||
set_hotspot()
|
||||
|
||||
print("Starting Flask server")
|
||||
print("----------------------------")
|
||||
wf_list = scan_wifi()
|
||||
print("----------------------------")
|
||||
print(wf_list)
|
||||
|
||||
|
||||
'''
|
||||
# 启动 Flask 服务器
|
||||
app.run(host='0.0.0.0', port=80, debug=True)
|
||||
'''
|
||||
|
Loading…
Reference in New Issue