Show current DNS using dhcpcd for currently used SSID

parent 2303d2fc
Pipeline #53529273 passed with stage
in 1 minute and 36 seconds
......@@ -6,6 +6,7 @@ from shutil import which
from sys import exit, stdout, argv
from typing import Optional, List, Dict
from re import sub
import os
def colorize(code: int, content: str) -> str:
......@@ -52,15 +53,39 @@ def gather_wireless_info(output: List[str]) -> List[Dict[str, str]]:
return networks
def get_executable_or_die(name: str) -> str:
def get_executable_or(name: str, die: bool = True) -> Optional[str]:
executable = which(name)
if executable is None:
print(colorize(31, f"Executable `{name}` cannot be found in $PATH!"))
exit(1)
if die:
print(colorize(31, f"Executable `{name}` cannot be found in $PATH!"))
exit(1)
return None
return executable
def parse_dns(interface: str) -> Optional[str]:
dhcpcd = get_executable_or("dhcpcd", False)
wifi_dns = None
if dhcpcd is None:
wifi_dns = colorize(31, "Cannot determine current DNS due to a missing dhcpcd")
else:
with open(os.devnull, 'w+') as nulldescr:
dhcpcd_out = run(
[f"{dhcpcd}", "-U", f"{interface}"],
stdout=PIPE,
stderr=nulldescr
).stdout.decode("utf-8")
for line in dhcpcd_out.split("\n"):
key, value = line.split("=", 1)
if key == "domain_name_servers":
wifi_dns = value
break
return wifi_dns
def main() -> None:
parser = ArgumentParser(description="List available wifi networks", prog=argv[0])
parser.add_argument("interface", default="wlan0", help="Which interface to query")
......@@ -68,7 +93,7 @@ def main() -> None:
args = parser.parse_args()
iwlist_exec = get_executable_or_die('iwlist')
iwlist_exec = get_executable_or('iwlist')
try:
output = run(
[f"{iwlist_exec}", f"{args.interface}", "scan"],
......@@ -85,7 +110,7 @@ def main() -> None:
print(colorize(31, f"No wireless networks found for interface {colorize(1, args.interface)}"))
exit(1)
iwgetid_exec = get_executable_or_die('iwgetid')
iwgetid_exec = get_executable_or('iwgetid')
current_ssid = run(
[f"{iwgetid_exec}", "-r"],
stdout=PIPE,
......@@ -97,7 +122,18 @@ def main() -> None:
if 'auth' not in n:
n['auth'] = colorize(31, "None")
current_indicator = colorize(1, '*') if current_ssid == n['ssid'] else " "
if current_ssid == n['ssid']:
current_indicator = colorize(1, '*')
wifi_dns = parse_dns(args.interface)
if wifi_dns is None:
wifi_dns = colorize(
33,
f"Cannot determine dns for {current_ssid}, info not in dhcpcd output"
)
else:
current_indicator = " "
wifi_dns = " "
print("{0} {1}: {2}".format(
current_indicator,
......@@ -107,6 +143,9 @@ def main() -> None:
print(" {0}: {1} {2}".format(colorize(1, "Secured"), n['enc'], '(%s)' % n['auth']))
if current_indicator != " ":
print(" {0}: {1}".format(colorize(1, "DNS"), wifi_dns))
print(" {0}: {1}{2}".format(
colorize(1, "Frequency"),
n['freq'],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment