Skip to content

Commit a465e66

Browse files
authored
Wifi Networks (#13)
1 parent 6259385 commit a465e66

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

obsidian-wizard.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ def start_iwd_service():
683683
return True
684684
except subprocess.CalledProcessError:
685685
return False
686-
elif shutil.which("iwd") or shutil.which("iwctl"):
686+
elif shutil.which("iwctl"):
687687
try:
688688
subprocess.run(["systemctl", "start", "iwd"], check=True)
689689
time.sleep(2)
@@ -715,7 +715,7 @@ def _get_wifi_interface():
715715

716716

717717
def get_wifi_networks():
718-
if shutil.which("iwctl") and shutil.which("iwd") and not shutil.which("rc-service"):
718+
if shutil.which("iwctl") and not shutil.which("rc-service"):
719719
try:
720720
iface = "wlan0"
721721
subprocess.run(["iwctl", "station", iface, "scan"],
@@ -727,12 +727,28 @@ def get_wifi_networks():
727727
)
728728
if result.returncode == 0:
729729
networks = []
730-
for line in result.stdout.split("\n")[4:]:
731-
if line.strip() and ("PSK" in line or "Open" in line):
732-
parts = line.split()
733-
if parts:
734-
ssid = parts[0]
735-
security = "PSK" if "PSK" in line else "Open"
730+
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
731+
clean_stdout = ansi_escape.sub('', result.stdout)
732+
lines = clean_stdout.splitlines()
733+
dash_count = 0
734+
for line in lines:
735+
if "---" in line:
736+
dash_count += 1
737+
continue
738+
if dash_count < 2:
739+
continue
740+
line_clean = line.replace('>', ' ').strip()
741+
if not line_clean:
742+
continue
743+
parts = re.split(r'\s{2,}', line_clean)
744+
if len(parts) >= 2:
745+
ssid = parts[0].strip()
746+
security = "Open"
747+
if "psk" in line.lower():
748+
security = "PSK"
749+
elif "8021x" in line.lower():
750+
security = "802.1X"
751+
if ssid:
736752
networks.append(f"{ssid} ({security})")
737753
return networks
738754
except Exception:
@@ -765,11 +781,11 @@ def get_wifi_networks():
765781

766782

767783
def connect_wifi(ssid, password=None):
768-
if shutil.which("iwctl") and shutil.which("iwd") and not shutil.which("rc-service"):
784+
if shutil.which("iwctl") and not shutil.which("rc-service"):
769785
try:
770786
if password:
771787
result = subprocess.run(
772-
["iwctl", "--password", password, "station", "wlan0", "connect", ssid],
788+
["iwctl", "--passphrase", password, "station", "wlan0", "connect", ssid],
773789
capture_output=True, text=True, timeout=15,
774790
)
775791
else:
@@ -1114,7 +1130,10 @@ def reboot_system():
11141130
"The system will boot into the newly installed/updated system",
11151131
],
11161132
):
1117-
run_command("sudo reboot", "Rebooting system")
1133+
if shutil.which("openrc-shutdown"):
1134+
run_command("sudo openrc-shutdown -r 5", "Rebooting system")
1135+
else:
1136+
run_command("sudo reboot", "Rebooting system")
11181137

11191138

11201139
def main():

0 commit comments

Comments
 (0)