From 78483258e86ede220d1e46fef04fb6894ac721d8 Mon Sep 17 00:00:00 2001 From: ericbsd Date: Sat, 17 Feb 2024 20:00:11 -0400 Subject: [PATCH] Rework auto-switch.py, setup-nic.py, networkmgr.conf Updated the list of wifi in setup-nic.py --- src/auto-switch.py | 74 ++++++++++++++------------------------------- src/networkmgr.conf | 8 ++++- src/setup-nic.py | 45 ++++++++++++++++----------- 3 files changed, 57 insertions(+), 70 deletions(-) diff --git a/src/auto-switch.py b/src/auto-switch.py index 23c22a0..ad5bee4 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -1,4 +1,7 @@ #!/usr/local/bin/python3 +""" +auto-switch - is used to automatically switches the default interface go down. +""" import sys import os @@ -10,12 +13,20 @@ exit() nic = args[1] -cmd = ["kenv", "-q", "rc_system"] -rc_system = Popen(cmd, stdout=PIPE, universal_newlines=True).stdout.read() -openrc = 'openrc' in rc_system +not_nics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|wlan" \ + r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)" -cmd = 'netstat -rn | grep default' -defautl_nic = Popen(cmd, stdout=PIPE, shell=True, universal_newlines=True).stdout.read() +default_nic = Popen( + 'netstat -rn | grep default', + shell=True, + universal_newlines=True +).stdout.read() + +# Stop the script if the nic is not valid or not in the default route. +if re.search(not_nics_regex, nic): + exit(0) +elif nic not in default_nic: + exit(0) nic_ifconfig = Popen( ['ifconfig', nic], @@ -24,54 +35,15 @@ universal_newlines=True ).stdout.read() -# Only stop dhclient if the status is not active or associated active_status = ( 'status: active' in nic_ifconfig, 'status: associated' in nic_ifconfig ) -if not any(active_status): - if openrc: - os.system(f'service dhcpcd.{nic} stop') - else: - if 'wlan' in nic: - os.system(f'service dhclient stop {nic}') - else: - os.system(f'service netif stop {nic}') - os.system('service routing restart') - -nics = Popen( - ['ifconfig', '-l', 'ether'], - stdout=PIPE, - close_fds=True, - universal_newlines=True -) - -notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ - r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)" - -nics_lelfover = nics.stdout.read().replace(nic, '').strip() -nic_list = sorted(re.sub(notnics_regex, '', nics_lelfover).strip().split()) -if not nic_list: - exit() - -for current_nic in nic_list: - output = Popen( - ['ifconfig', current_nic], - stdout=PIPE, - close_fds=True, - universal_newlines=True - ) - nic_ifconfig = output.stdout.read() - status_types = [ - 'active', - 'associated', - ] - found_status = re.search(f"status: ({'|'.join(status_types)})", nic_ifconfig) - found_inet = re.search("inet(\s|6)", nic_ifconfig) - if found_status and found_inet: - if openrc: - os.system(f'service dhcpcd.{current_nic} restart') - else: - os.system(f'service dhclient restart {current_nic}') - break +# Stop the interface if it's not active or associated. +# This removes the interface from the default route. +# Restarting routing adds and nic if there is and other one that is active +# or associated. +if not any(active_status): + os.system(f'service netif stop {nic}') + os.system('service routing restart') diff --git a/src/networkmgr.conf b/src/networkmgr.conf index 23d67c3..4ab6061 100644 --- a/src/networkmgr.conf +++ b/src/networkmgr.conf @@ -3,14 +3,20 @@ notify 100 { match "system" "IFNET"; match "subsystem" "!(usbus|wlan)[0-9]+"; match "type" "ATTACH"; + action "/usr/local/share/networkmgr/setup-nic.py $subsystem"; +}; +notify 100 { + match "system" "IFNET"; + match "type" "LINK_UP"; + media-type "ethernet"; action "/usr/local/share/networkmgr/setup-nic.py $subsystem"; }; notify 100 { match "system" "IFNET"; + match "subsystem" "!(usbus|wlan)[0-9]+"; match "type" "LINK_DOWN"; - action "/usr/local/share/networkmgr/auto-switch.py $subsystem"; }; diff --git a/src/setup-nic.py b/src/setup-nic.py index 70d575a..512c9bb 100755 --- a/src/setup-nic.py +++ b/src/setup-nic.py @@ -5,6 +5,7 @@ import shutil import sys from pathlib import Path +from subprocess import Popen, PIPE def file_content(paths): @@ -21,25 +22,25 @@ def file_content(paths): nic = args[1] etc = Path(os.sep, "etc") -rcconf = etc / "rc.conf" -rcconflocal = etc / "rc.conf.local" +rc_conf = etc / "rc.conf" +rc_conf_local = etc / "rc.conf.local" wpa_supplicant = etc / "wpa_supplicant.conf" -rcconf_paths = [rcconf] +rc_conf_paths = [rc_conf] -if rcconflocal.exists(): - rcconf_paths.append(rcconflocal) +if rc_conf_local.exists(): + rc_conf_paths.append(rc_conf_local) -rcconf_content = file_content(rcconf_paths) +rc_conf_content = file_content(rc_conf_paths) -notnics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ +not_nics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ "ppp|bridge|wg|wlan)[0-9]+|vm-[a-z]+" # wifi_driver_regex is taken from devd.conf wifi-driver-regex -wifi_driver_regex = "(ath|bwi|bwn|ipw|iwlwifi|iwi|iwm|iwn|malo|mwl|otus|" \ +wifi_driver_regex = "(ath|ath[0-9]+k|bwi|bwn|ipw|iwlwifi|iwi|iwm|iwn|malo|mwl|mt79|otus|" \ "ral|rsu|rtw|rtwn|rum|run|uath|upgt|ural|urtw|wpi|wtap|zyd)[0-9]+" -if re.search(notnics_regex, nic): +if re.search(not_nics_regex, nic): exit(0) if re.search(wifi_driver_regex, nic): @@ -48,14 +49,22 @@ def file_content(paths): shutil.chown(wpa_supplicant, user="root", group="wheel") wpa_supplicant.chmod(0o765) for wlanNum in range(0, 9): - if f'wlan{wlanNum}' not in rcconf_content: - break - if f'wlans_{nic}=' not in rcconf_content: - with rcconf.open('a') as rc: - rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n') - rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n') + if f'wlan{wlanNum}' not in rc_conf_content: + if f'wlans_{nic}=' not in rc_conf_content: + with rc_conf.open('a') as rc: + rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n') + rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n') + Popen(f'/etc/pccard_ether {nic} startchildren', shell=True) + else: - if f'ifconfig_{nic}=' not in rcconf_content: - with rcconf.open('a') as rc: + if f'ifconfig_{nic}=' not in rc_conf_content: + with rc_conf.open('a') as rc: rc.writelines(f'ifconfig_{nic}="DHCP"\n') -os.system(f'/etc/pccard_ether {nic} startchildren') + Popen('/etc/pccard_ether {nic} startchildren', shell=True) + else: + Popen( + f'service netif start {nic} ; ' + f'service dhclient start {nic} ; ' + f'service routing restart', + shell=True + )