68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
# sudo nmcli device wifi connect "Home" ifname wlxf81a6719febb password "Dragon1234"
|
|
# sudo nmcli -t -f SSID,SIGNAL,IN-USE,SECURITY -e yes -m tab device wifi list ifname wlxf81a6719febb --rescan yes
|
|
|
|
import subprocess
|
|
import sys
|
|
|
|
from flask import Flask
|
|
from flask import request
|
|
|
|
CMD_SCAN = "sudo nmcli -t -f SSID,SIGNAL,IN-USE,SECURITY -e yes -m tab device wifi list ifname wlxf81a6719febb --rescan yes"
|
|
scan = ["rpi:100: :WPA2", "Home:94:*:WPA2", "HOME2:48: :WPA2", "BT:23: :"]
|
|
|
|
app = Flask(__name__)
|
|
|
|
# sudo nmcli device wifi connect "HOME2" ifname wlxf81a6719febb password "Dragon123"
|
|
# Device 'wlxf81a6719febb' successfully activated with 'ffbc2621-c0d8-4975-8509-92c1740be6a7'.
|
|
# rich@rpi:~$ echo $?
|
|
# 0
|
|
# rich@rpi:~$ sudo nmcli device wifi connect "HOME2" ifname wlxf81a6719febb password "Dragon1234"
|
|
# Error: Connection activation failed: (7) Secrets were required, but not provided.
|
|
|
|
|
|
def scan_wifi():
|
|
scan = []
|
|
temp = subprocess.run(CMD_SCAN.split(" "), stdout=subprocess.PIPE).stdout.decode("utf-8")
|
|
for line in temp.split():
|
|
if line[0] != "":
|
|
scan.append(line)
|
|
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
def main():
|
|
if request.method == "GET":
|
|
# scan_wifi()
|
|
output = """<form method="POST"><table>
|
|
<thead>
|
|
<tr>
|
|
<th>Select</th>
|
|
<th>SID</th>
|
|
<th>Strength</th>
|
|
<th>Connected</th>
|
|
<th>Password</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
*DATA*
|
|
</tbody>
|
|
<p><input type=submit value=Connect></p></form>"""
|
|
table = ""
|
|
counter = 1
|
|
for line in scan:
|
|
line = line.split(":")
|
|
table += '<tr><td><input value = "{}" id="type_radio_{}" name="type_radio" type="radio"</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>'.format(
|
|
counter,
|
|
counter,
|
|
line[0],
|
|
line[1],
|
|
"yes" if line[2].strip() == "*" else "",
|
|
"None" if line[3].strip() == "" else line[3],
|
|
)
|
|
counter += 1
|
|
output = output.replace("*DATA*", table)
|
|
return output
|
|
if request.method == "POST":
|
|
line = scan[int(request.form.get("type_radio")) - 1]
|
|
|
|
return line
|