New Version
This commit is contained in:
157
app/network_utils.py
Normal file
157
app/network_utils.py
Normal file
@@ -0,0 +1,157 @@
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from config import ClientInterface
|
||||
|
||||
wpafile_wpa = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
|
||||
network={{
|
||||
ssid="{}"
|
||||
psk="{}"
|
||||
key_mgmt=WPA-PSK
|
||||
}}
|
||||
"""
|
||||
|
||||
wpafile_nowpa = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
|
||||
network={{
|
||||
ssid="{}"
|
||||
key_mgmt=NONE
|
||||
}}
|
||||
"""
|
||||
|
||||
wpafile_none = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
"""
|
||||
|
||||
|
||||
def run_subprocess(cmd, check=True):
|
||||
cmd_split = cmd.split(" ")
|
||||
return subprocess.run(cmd_split, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=check).stdout.decode("utf-8")
|
||||
|
||||
|
||||
def run_subprocess_interface(cmd, check=True):
|
||||
return run_subprocess(cmd.format(ClientInterface), check)
|
||||
|
||||
|
||||
def parse_iwlist(iwlist_output, current_sid):
|
||||
"""Parses iwlist scan output
|
||||
|
||||
Args:
|
||||
iwlist_output (string): iwlist scan output
|
||||
current (string): Current Connected Sidd
|
||||
|
||||
Returns:
|
||||
[dictionary]: Dictionary containing relavent data
|
||||
"""
|
||||
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 = []
|
||||
|
||||
try:
|
||||
for item in data:
|
||||
cell = {}
|
||||
for line in item:
|
||||
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_sid:
|
||||
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)
|
||||
except:
|
||||
cells = []
|
||||
|
||||
return cells
|
||||
|
||||
|
||||
def scan_networks():
|
||||
"""Scans for Networks"""
|
||||
output = run_subprocess("sudo iwgetid", check=False)
|
||||
|
||||
current = output.partition("ESSID:")[2].strip().strip('"')
|
||||
|
||||
output = run_subprocess("sudo iwlist {} scan".format(ClientInterface))
|
||||
|
||||
scan = parse_iwlist(output, current)
|
||||
|
||||
return scan
|
||||
|
||||
|
||||
def connect_network(ssid, security, password):
|
||||
with open("/etc/network/interfaces", "r") as source:
|
||||
lines = source.readlines()
|
||||
with open("/etc/network/interfaces", "w") as source:
|
||||
for line in lines:
|
||||
source.write(
|
||||
re.sub(r"^iface {} inet manual".format(ClientInterface), "iface {} inet dhcp".format(ClientInterface), line)
|
||||
)
|
||||
|
||||
run_subprocess_interface("sudo /usr/sbin/ifdown {}")
|
||||
|
||||
run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant")
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
if security == "WPA2":
|
||||
f.write(wpafile_wpa.format(ssid, password))
|
||||
else:
|
||||
f.write(wpafile_nowpa)
|
||||
|
||||
try:
|
||||
run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant")
|
||||
run_subprocess_interface("sudo /usr/sbin/ifup {}")
|
||||
except:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def disconnect_network(ssid):
|
||||
run_subprocess_interface("sudo /usr/sbin/ifdown {}")
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile_none)
|
||||
|
||||
run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant")
|
||||
|
||||
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 {} inet dhcp".format(ClientInterface), "iface {} inet manual".format(ClientInterface), line)
|
||||
)
|
||||
|
||||
try:
|
||||
run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant")
|
||||
run_subprocess_interface("sudo /usr/sbin/ifup {}")
|
||||
except:
|
||||
return False
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user