sane-ip-reconnect: add some logging
This commit is contained in:
parent
9f55a8288d
commit
4eb6f59b01
|
@ -1,10 +1,17 @@
|
||||||
#!/usr/bin/env nix-shell
|
#!/usr/bin/env nix-shell
|
||||||
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ])"
|
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ])"
|
||||||
|
# vim: set filetype=python :
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def rm_color(stdout: str) -> str:
|
def rm_color(stdout: str) -> str:
|
||||||
" remove terminal control codes -- used by iwctl to colorize the output "
|
" remove terminal control codes -- used by iwctl to colorize the output "
|
||||||
return re.sub("\\[[0-9;]*m", "", stdout)
|
return re.sub("\\[[0-9;]*m", "", stdout)
|
||||||
|
@ -27,6 +34,8 @@ def extract_columns(stdout: str) -> list:
|
||||||
# the `>` is formatting, indicating that it's the active network
|
# the `>` is formatting, indicating that it's the active network
|
||||||
first = first[2:].strip()
|
first = first[2:].strip()
|
||||||
items.append((first, second))
|
items.append((first, second))
|
||||||
|
|
||||||
|
logger.debug(f"parsed iwctl output: {items!r}")
|
||||||
return items
|
return items
|
||||||
|
|
||||||
def iwctl(args: list, sudo: bool = False) -> str:
|
def iwctl(args: list, sudo: bool = False) -> str:
|
||||||
|
@ -47,12 +56,14 @@ def get_known() -> list:
|
||||||
stdout = iwctl(["known-networks", "list"])
|
stdout = iwctl(["known-networks", "list"])
|
||||||
stdout = rm_color(stdout)
|
stdout = rm_color(stdout)
|
||||||
stdout = rm_heading(stdout)
|
stdout = rm_heading(stdout)
|
||||||
|
logging.debug(f"iwctl known-networks list: got: {stdout}")
|
||||||
return [name for (name, date) in extract_columns(stdout)]
|
return [name for (name, date) in extract_columns(stdout)]
|
||||||
|
|
||||||
def get_visible() -> list:
|
def get_visible() -> list:
|
||||||
stdout = iwctl(["station", "wlan0", "get-networks", "rssi-dbms"])
|
stdout = iwctl(["station", "wlan0", "get-networks", "rssi-dbms"])
|
||||||
stdout = rm_color(stdout)
|
stdout = rm_color(stdout)
|
||||||
stdout = rm_heading(stdout)
|
stdout = rm_heading(stdout)
|
||||||
|
logging.debug(f"iwctl station wlan0 get-networks rssi-dbms: got: {stdout}")
|
||||||
return [(name, int(strength)) for (name, strength) in extract_columns(stdout)]
|
return [(name, int(strength)) for (name, strength) in extract_columns(stdout)]
|
||||||
|
|
||||||
def choose_best(visible: list, known: list) -> str:
|
def choose_best(visible: list, known: list) -> str:
|
||||||
|
@ -71,26 +82,40 @@ def restart() -> str:
|
||||||
"iwd",
|
"iwd",
|
||||||
])
|
])
|
||||||
|
|
||||||
print("scanning for networks... ", end="", flush=True)
|
def main():
|
||||||
scan()
|
print("scanning for networks... ", end="", flush=True)
|
||||||
print("done")
|
scan()
|
||||||
print()
|
print("done")
|
||||||
|
print()
|
||||||
|
|
||||||
known = get_known()
|
known = get_known()
|
||||||
print("known networks:", "".join(f"\n\t{name}" for name in known))
|
print("known networks:", "".join(f"\n\t{name}" for name in known))
|
||||||
print()
|
print()
|
||||||
|
|
||||||
visible = get_visible()
|
visible = get_visible()
|
||||||
print("visible networks:", "".join(f"\n\t{name}: {rss}" for (name, rss) in visible))
|
print("visible networks:", "".join(f"\n\t{name}: {rss}" for (name, rss) in visible))
|
||||||
print()
|
print()
|
||||||
|
|
||||||
best = choose_best(visible, known)
|
best = choose_best(visible, known)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(f"connecting to {best}")
|
print(f"connecting to {best}")
|
||||||
result = connect(best)
|
result = connect(best)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print("restarting iwd daemon")
|
print("restarting iwd daemon")
|
||||||
restart()
|
restart()
|
||||||
else:
|
else:
|
||||||
print("success", result)
|
print("success", result)
|
||||||
|
|
||||||
|
def parse_args(args: list):
|
||||||
|
for a in args:
|
||||||
|
if a in ("-v", "--verbose"):
|
||||||
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"unexpected arg: {a}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
logging.basicConfig()
|
||||||
|
parse_args(sys.argv[1:])
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user