Working before re-write, no nord
This commit is contained in:
147
app/routes.py
147
app/routes.py
@@ -1,12 +1,13 @@
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from config import RPI, Debug, ClientInterface
|
||||
from config import RPI, ClientInterface, Debug
|
||||
from flask import flash, redirect, render_template, url_for
|
||||
from flask_login import current_user, login_required, login_user, logout_user
|
||||
|
||||
from app import app
|
||||
from app.forms import LoginForm, WPAForm
|
||||
from app.models import User, Passwords
|
||||
from app.models import Passwords, User
|
||||
|
||||
CMD_SCAN = "sudo nmcli -t -f SSID,SIGNAL,IN-USE,SECURITY -e yes -m tab device wifi list ifname wlan1 --rescan yes"
|
||||
CMD_JOIN = "sudo nmcli device wifi connect *SSID* ifname wlan1"
|
||||
@@ -16,6 +17,48 @@ CMD_DISCONNECT = "sudo nmcli device disconnect wlan1"
|
||||
# Device 'wlxf81a6719febb' successfully activated with '11111-1111-11111-111111-11111111'
|
||||
|
||||
|
||||
def parse_iwlist(iwlist_output, current):
|
||||
data = []
|
||||
cell = []
|
||||
for line in iwlist_output.splitlines():
|
||||
if line.find(" Cell ") != -1 and cell != []:
|
||||
data.append(cell)
|
||||
cell = []
|
||||
elif line.find("Scan completed :") > 0:
|
||||
pass
|
||||
else:
|
||||
cell.append(line)
|
||||
|
||||
try:
|
||||
del data[0][0]
|
||||
except:
|
||||
pass
|
||||
|
||||
cells = []
|
||||
|
||||
for a in data:
|
||||
cell = {}
|
||||
for line in a:
|
||||
line = line.strip()
|
||||
if line.find("ESSID:") != -1:
|
||||
if line.partition("ESSID:")[2].strip('"') != "":
|
||||
cell["SSID"] = line.partition("ESSID:")[2].strip('"')
|
||||
if cell["SSID"] == current:
|
||||
cell["Connected"] = "Yes"
|
||||
else:
|
||||
cell["Connected"] = ""
|
||||
if line.partition("Signal level=")[2].split("/")[0] != "" != -1:
|
||||
cell["Signal"] = line.partition("Signal level=")[2].split("/")[0]
|
||||
if line.find("Encryption key:") != -1:
|
||||
if line.find(":on") != -1:
|
||||
cell["WPA"] = "WPA2"
|
||||
else:
|
||||
cell["WPA"] = "None"
|
||||
cells.append(cell)
|
||||
|
||||
return cells
|
||||
|
||||
|
||||
def scan_networks():
|
||||
scan = []
|
||||
|
||||
@@ -30,28 +73,29 @@ def scan_networks():
|
||||
current = output.partition("ESSID:")[2].strip().strip('"')
|
||||
|
||||
output = subprocess.run(["sudo", "iwlist", ClientInterface, "scan"], stdout=subprocess.PIPE).stdout.decode("utf-8")
|
||||
a = 0
|
||||
scan = []
|
||||
for line in output.splitlines():
|
||||
if a == 0:
|
||||
connected = " "
|
||||
if line.partition("ESSID:")[2].strip('"') != "":
|
||||
a = 1
|
||||
ssid = line.partition("ESSID:")[2].strip('"')
|
||||
if ssid == current:
|
||||
connected = "*"
|
||||
if a == 1:
|
||||
if line.find("Encryption key:off") != -1:
|
||||
password = ""
|
||||
a = 2
|
||||
elif line.find("Encryption key:on") != -1:
|
||||
password = "WPA2"
|
||||
a = 2
|
||||
if a == 2:
|
||||
if line.partition("Signal level=")[2].split("/")[0] != "":
|
||||
signal = line.partition("Signal level=")[2].split("/")[0]
|
||||
a = 0
|
||||
scan.append(ssid + ":" + signal + ":" + connected + ":" + password)
|
||||
scan = parse_iwlist(output, current)
|
||||
# a = 0
|
||||
# scan = []
|
||||
# for line in output.splitlines():
|
||||
# if a == 0:
|
||||
# connected = " "
|
||||
# if line.partition("ESSID:")[2].strip('"') != "":
|
||||
# a = 1
|
||||
# ssid = line.partition("ESSID:")[2].strip('"')
|
||||
# if ssid == current:
|
||||
# connected = "*"
|
||||
# if a == 1:
|
||||
# if line.find("Encryption key:off") != -1:
|
||||
# password = ""
|
||||
# a = 2
|
||||
# elif line.find("Encryption key:on") != -1:
|
||||
# password = "WPA2"
|
||||
# a = 2
|
||||
# if a == 2:
|
||||
# if line.partition("Signal level=")[2].split("/")[0] != "":
|
||||
# signal = line.partition("Signal level=")[2].split("/")[0]
|
||||
# a = 0
|
||||
# scan.append(ssid + ":" + signal + ":" + connected + ":" + password)
|
||||
return scan
|
||||
|
||||
|
||||
@@ -64,13 +108,18 @@ def index():
|
||||
if not Debug:
|
||||
scan = scan_networks()
|
||||
|
||||
# for network in scan:
|
||||
# item = [
|
||||
# network.split(":", maxsplit=1)[0],
|
||||
# network.split(":")[1],
|
||||
# "Yes" if network.split(":")[2] == "*" else "",
|
||||
# "None" if network.split(":")[3].strip() == "" else "WPA2",
|
||||
# ]
|
||||
# results.append(item)
|
||||
|
||||
item = []
|
||||
for network in scan:
|
||||
item = [
|
||||
network.split(":", maxsplit=1)[0],
|
||||
network.split(":")[1],
|
||||
"Yes" if network.split(":")[2] == "*" else "",
|
||||
"None" if network.split(":")[3].strip() == "" else "WPA2",
|
||||
]
|
||||
item = [network["SSID"], network["Signal"], network["Connected"], network["WPA"]]
|
||||
results.append(item)
|
||||
|
||||
# table = Networks(results)
|
||||
@@ -134,6 +183,13 @@ network={{
|
||||
""".format(
|
||||
ssid, form.password.data
|
||||
)
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(re.sub(r"^iface wlan0 inet manual", "iface wlan0 inet dhcp", line))
|
||||
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifdown", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
@@ -192,6 +248,12 @@ network={{
|
||||
ssid
|
||||
)
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(re.sub(r"^iface wlan0 inet manual", "iface wlan0 inet dhcp", line))
|
||||
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifdown", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
@@ -232,4 +294,29 @@ def disconnect(ssid):
|
||||
["sudo", "/usr/sbin/ifdown", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
|
||||
wpafile = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
"""
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile)
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(re.sub(r"^iface wlan0 inet dhcp", "iface wlan0 inet manual", line))
|
||||
|
||||
try:
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/bin/systemctl", "restart", "wpa_supplicant"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=20,
|
||||
).stdout.decode("utf-8")
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifup", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20
|
||||
).stdout.decode("utf-8")
|
||||
except:
|
||||
return render_template("message.html", message="Failt to disconnect from {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid))
|
||||
|
||||
Reference in New Issue
Block a user