sane-ip-reconnect: be more resilient to errors
This commit is contained in:
parent
79fca6c5e5
commit
f71fd4565b
|
@ -48,19 +48,26 @@ def iwctl(args: list[str], sudo: bool = False) -> str:
|
|||
res.check_returncode() # raise
|
||||
return res.stdout.decode()
|
||||
|
||||
def try_iwctl(args: list[str], sudo: bool = False, default: any = None) -> any:
|
||||
try:
|
||||
return iwctl(args, sudo)
|
||||
except subprocess.CalledProcessError:
|
||||
print("failed to iwctl: {args}")
|
||||
return default
|
||||
|
||||
def scan() -> None:
|
||||
iwctl(["station", "wlan0", "scan"], sudo=True)
|
||||
try_iwctl(["station", "wlan0", "scan"], sudo=True)
|
||||
time.sleep(5) # give time for adapter to see networks
|
||||
|
||||
def get_known() -> list[str]:
|
||||
stdout = iwctl(["known-networks", "list"])
|
||||
stdout = try_iwctl(["known-networks", "list"], default="")
|
||||
stdout = rm_color(stdout)
|
||||
stdout = rm_heading(stdout)
|
||||
logging.debug(f"iwctl known-networks list: got: {stdout}")
|
||||
return [name for (name, date) in extract_columns(stdout)]
|
||||
|
||||
def get_visible() -> list[tuple[str, int]]:
|
||||
stdout = iwctl(["station", "wlan0", "get-networks", "rssi-dbms"])
|
||||
stdout = try_iwctl(["station", "wlan0", "get-networks", "rssi-dbms"], default="")
|
||||
stdout = rm_color(stdout)
|
||||
stdout = rm_heading(stdout)
|
||||
logging.debug(f"iwctl station wlan0 get-networks rssi-dbms: got: {stdout}")
|
||||
|
@ -74,14 +81,20 @@ def choose_best(visible: list[tuple[str, int]], known: list[str]) -> str:
|
|||
def connect(network: str) -> str:
|
||||
return iwctl(["station", "wlan0", "connect", network], sudo=True)
|
||||
|
||||
def restart(unit: str = "iwd") -> str:
|
||||
return subprocess.check_output([
|
||||
def restart(unit: str = "iwd") -> None:
|
||||
subprocess.check_output([
|
||||
"sudo",
|
||||
"systemctl",
|
||||
"restart",
|
||||
unit,
|
||||
])
|
||||
|
||||
def try_restart(unit: str = "iwd") -> None:
|
||||
try:
|
||||
restart(unit)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"failed to restart {unit}")
|
||||
|
||||
def try_connect(network: str | None) -> bool:
|
||||
"""returns True on successful connection"""
|
||||
if network is None:
|
||||
|
@ -116,9 +129,9 @@ def main() -> None:
|
|||
|
||||
if not try_connect(best):
|
||||
print("restarting iwd daemon")
|
||||
restart()
|
||||
try_restart()
|
||||
print("reimporting secrets")
|
||||
restart("ensure-var-lib-iwd-.secrets.psk.stamp")
|
||||
try_restart("ensure-var-lib-iwd-.secrets.psk.stamp")
|
||||
|
||||
def parse_args(args: list[str]) -> None:
|
||||
for a in args:
|
||||
|
|
Loading…
Reference in New Issue
Block a user