This commit is contained in:
Rich
2021-07-08 18:33:23 +01:00
parent 33ee8edb6c
commit f3559b729b
2 changed files with 49 additions and 17 deletions

View File

@@ -1,13 +1,12 @@
import subprocess
from pprint import pformat
from config import RPI, 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 Network, Networks, User
from config import debug, RPI
CMD_SCAN = "sudo nmcli -t -f SSID,SIGNAL,IN-USE,SECURITY -e yes -m tab device wifi list ifname wlxf81a6719febb --rescan yes"
CMD_JOIN = "sudo nmcli device wifi connect *SSID* ifname wlxf81a6719febb"
@@ -107,19 +106,47 @@ def logout():
@app.route("/wpa/<ssid>", methods=["GET", "POST"])
@login_required
def wpa(ssid):
form = WPAForm()
if form.validate_on_submit():
cmd = CMD_JOINPW.replace("*SSID*", ssid)
cmd = cmd.replace("*PASSWORD*", form.password.data)
# wlan0: flags=4098<BROADCAST,MULTICAST> mtu 1500
# wlan1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
if not RPI:
form = WPAForm()
if form.validate_on_submit():
cmd = CMD_JOINPW.replace("*SSID*", ssid)
cmd = cmd.replace("*PASSWORD*", form.password.data)
output = subprocess.run(cmd.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
if output.find("Error") == -1 and output != "":
return render_template("message.html", message="Successfully connected to {}".format(ssid))
return render_template("message.html", message="Failed to connect to {}".format(ssid))
return render_template("wpa.html", title="WPA Password", form=form)
else:
form = WPAForm()
if form.validate_on_submit():
wpa = '''country=GB # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={{
ssid="{}"
psk="{}"
key_mgmt=WPA-PSK
}}
'''.format(ssid,form.password.data)
with open('/tmp/arg.txt','wt') as f:
f.write(wpa)
output = subprocess.run(['sudo', '/usr/sbin/ifdown','wlan0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
output = subprocess.run(['sudo', '/usr/bin/systemctl','stop','wpa_supplicant'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
output = subprocess.run(['sudo', 'mv','/tmp/arg/txt','/etc/wpa_supplicant/wpa_supplicant.conf'], shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
try:
output = subprocess.run(['sudo', '/usr/sbin/ifup','wlan0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,timeout=10).stdout.decode("utf-8")
output = subprocess.run(['sudo', '/usr/bin/systemctl','start','wpa_supplicant'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
except:
return render_template("message.html", message="Failt to connected to {}".format(ssid))
output = subprocess.run(cmd.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
if output.find("Error") == -1 and output != "":
return render_template("message.html", message="Successfully connected to {}".format(ssid))
return render_template("message.html", message="Failed to connect to {}".format(ssid))
return render_template("wpa.html", title="WPA Password", form=form)
return render_template("wpa.html", title="WPA Password", form=form)
@app.route("/connect/<string:ssid>&<string:security>", methods=["GET", "POST"])
@login_required
@@ -139,8 +166,13 @@ def connect(ssid, security):
@app.route("/disconnect/<string:ssid>", methods=["GET", "POST"])
@login_required
def disconnect(ssid):
output = subprocess.run(CMD_DISCONNECT.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
if output.find("successfully disconnected") != -1:
return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid))
if not RPI:
output = subprocess.run(CMD_DISCONNECT.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
if output.find("successfully disconnected") != -1:
return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid))
return render_template("message.html", message="Failed to Disconnect from {}".format(ssid))
return render_template("message.html", message="Failed to Disconnect from {}".format(ssid))
else:
output = subprocess.run(['sudo','/usr/sbin/ifdown','wlan0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid))

View File

@@ -6,7 +6,7 @@
<thead>
<tr>
<th class="tg-0lax">SSID</th>
<th class="tg-0lax">Signal Size</th>
<th class="tg-0lax">Signle</th>
<th class="tg-0lax">Connected</th>
<th class="tg-0lax">Security</th>
<th class="tg-0lax">Action</th>