Skip to content

Commit

Permalink
Rework auto-switch.py, setup-nic.py, networkmgr.conf
Browse files Browse the repository at this point in the history
Updated the list of wifi in setup-nic.py
  • Loading branch information
ericbsd committed Feb 18, 2024
1 parent f0dddd1 commit 7848325
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 70 deletions.
74 changes: 23 additions & 51 deletions src/auto-switch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/local/bin/python3
"""
auto-switch - is used to automatically switches the default interface go down.
"""

import sys
import os
Expand All @@ -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],
Expand All @@ -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')
8 changes: 7 additions & 1 deletion src/networkmgr.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};

Expand Down
45 changes: 27 additions & 18 deletions src/setup-nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import sys
from pathlib import Path
from subprocess import Popen, PIPE


def file_content(paths):
Expand All @@ -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):
Expand All @@ -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
)

0 comments on commit 7848325

Please sign in to comment.