Skip to content

Commit 7848325

Browse files
committed
Rework auto-switch.py, setup-nic.py, networkmgr.conf
Updated the list of wifi in setup-nic.py
1 parent f0dddd1 commit 7848325

File tree

3 files changed

+57
-70
lines changed

3 files changed

+57
-70
lines changed

src/auto-switch.py

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/local/bin/python3
2+
"""
3+
auto-switch - is used to automatically switches the default interface go down.
4+
"""
25

36
import sys
47
import os
@@ -10,12 +13,20 @@
1013
exit()
1114
nic = args[1]
1215

13-
cmd = ["kenv", "-q", "rc_system"]
14-
rc_system = Popen(cmd, stdout=PIPE, universal_newlines=True).stdout.read()
15-
openrc = 'openrc' in rc_system
16+
not_nics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|wlan" \
17+
r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)"
1618

17-
cmd = 'netstat -rn | grep default'
18-
defautl_nic = Popen(cmd, stdout=PIPE, shell=True, universal_newlines=True).stdout.read()
19+
default_nic = Popen(
20+
'netstat -rn | grep default',
21+
shell=True,
22+
universal_newlines=True
23+
).stdout.read()
24+
25+
# Stop the script if the nic is not valid or not in the default route.
26+
if re.search(not_nics_regex, nic):
27+
exit(0)
28+
elif nic not in default_nic:
29+
exit(0)
1930

2031
nic_ifconfig = Popen(
2132
['ifconfig', nic],
@@ -24,54 +35,15 @@
2435
universal_newlines=True
2536
).stdout.read()
2637

27-
# Only stop dhclient if the status is not active or associated
2838
active_status = (
2939
'status: active' in nic_ifconfig,
3040
'status: associated' in nic_ifconfig
3141
)
32-
if not any(active_status):
33-
if openrc:
34-
os.system(f'service dhcpcd.{nic} stop')
35-
else:
36-
if 'wlan' in nic:
37-
os.system(f'service dhclient stop {nic}')
38-
else:
39-
os.system(f'service netif stop {nic}')
40-
os.system('service routing restart')
41-
42-
nics = Popen(
43-
['ifconfig', '-l', 'ether'],
44-
stdout=PIPE,
45-
close_fds=True,
46-
universal_newlines=True
47-
)
48-
49-
notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
50-
r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)"
51-
52-
nics_lelfover = nics.stdout.read().replace(nic, '').strip()
53-
nic_list = sorted(re.sub(notnics_regex, '', nics_lelfover).strip().split())
5442

55-
if not nic_list:
56-
exit()
57-
58-
for current_nic in nic_list:
59-
output = Popen(
60-
['ifconfig', current_nic],
61-
stdout=PIPE,
62-
close_fds=True,
63-
universal_newlines=True
64-
)
65-
nic_ifconfig = output.stdout.read()
66-
status_types = [
67-
'active',
68-
'associated',
69-
]
70-
found_status = re.search(f"status: ({'|'.join(status_types)})", nic_ifconfig)
71-
found_inet = re.search("inet(\s|6)", nic_ifconfig)
72-
if found_status and found_inet:
73-
if openrc:
74-
os.system(f'service dhcpcd.{current_nic} restart')
75-
else:
76-
os.system(f'service dhclient restart {current_nic}')
77-
break
43+
# Stop the interface if it's not active or associated.
44+
# This removes the interface from the default route.
45+
# Restarting routing adds and nic if there is and other one that is active
46+
# or associated.
47+
if not any(active_status):
48+
os.system(f'service netif stop {nic}')
49+
os.system('service routing restart')

src/networkmgr.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ notify 100 {
33
match "system" "IFNET";
44
match "subsystem" "!(usbus|wlan)[0-9]+";
55
match "type" "ATTACH";
6+
action "/usr/local/share/networkmgr/setup-nic.py $subsystem";
7+
};
68

9+
notify 100 {
10+
match "system" "IFNET";
11+
match "type" "LINK_UP";
12+
media-type "ethernet";
713
action "/usr/local/share/networkmgr/setup-nic.py $subsystem";
814
};
915

1016
notify 100 {
1117
match "system" "IFNET";
18+
match "subsystem" "!(usbus|wlan)[0-9]+";
1219
match "type" "LINK_DOWN";
13-
1420
action "/usr/local/share/networkmgr/auto-switch.py $subsystem";
1521
};
1622

src/setup-nic.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import sys
77
from pathlib import Path
8+
from subprocess import Popen, PIPE
89

910

1011
def file_content(paths):
@@ -21,25 +22,25 @@ def file_content(paths):
2122
nic = args[1]
2223

2324
etc = Path(os.sep, "etc")
24-
rcconf = etc / "rc.conf"
25-
rcconflocal = etc / "rc.conf.local"
25+
rc_conf = etc / "rc.conf"
26+
rc_conf_local = etc / "rc.conf.local"
2627
wpa_supplicant = etc / "wpa_supplicant.conf"
2728

28-
rcconf_paths = [rcconf]
29+
rc_conf_paths = [rc_conf]
2930

30-
if rcconflocal.exists():
31-
rcconf_paths.append(rcconflocal)
31+
if rc_conf_local.exists():
32+
rc_conf_paths.append(rc_conf_local)
3233

33-
rcconf_content = file_content(rcconf_paths)
34+
rc_conf_content = file_content(rc_conf_paths)
3435

35-
notnics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
36+
not_nics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
3637
"ppp|bridge|wg|wlan)[0-9]+|vm-[a-z]+"
3738

3839
# wifi_driver_regex is taken from devd.conf wifi-driver-regex
39-
wifi_driver_regex = "(ath|bwi|bwn|ipw|iwlwifi|iwi|iwm|iwn|malo|mwl|otus|" \
40+
wifi_driver_regex = "(ath|ath[0-9]+k|bwi|bwn|ipw|iwlwifi|iwi|iwm|iwn|malo|mwl|mt79|otus|" \
4041
"ral|rsu|rtw|rtwn|rum|run|uath|upgt|ural|urtw|wpi|wtap|zyd)[0-9]+"
4142

42-
if re.search(notnics_regex, nic):
43+
if re.search(not_nics_regex, nic):
4344
exit(0)
4445

4546
if re.search(wifi_driver_regex, nic):
@@ -48,14 +49,22 @@ def file_content(paths):
4849
shutil.chown(wpa_supplicant, user="root", group="wheel")
4950
wpa_supplicant.chmod(0o765)
5051
for wlanNum in range(0, 9):
51-
if f'wlan{wlanNum}' not in rcconf_content:
52-
break
53-
if f'wlans_{nic}=' not in rcconf_content:
54-
with rcconf.open('a') as rc:
55-
rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n')
56-
rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n')
52+
if f'wlan{wlanNum}' not in rc_conf_content:
53+
if f'wlans_{nic}=' not in rc_conf_content:
54+
with rc_conf.open('a') as rc:
55+
rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n')
56+
rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n')
57+
Popen(f'/etc/pccard_ether {nic} startchildren', shell=True)
58+
5759
else:
58-
if f'ifconfig_{nic}=' not in rcconf_content:
59-
with rcconf.open('a') as rc:
60+
if f'ifconfig_{nic}=' not in rc_conf_content:
61+
with rc_conf.open('a') as rc:
6062
rc.writelines(f'ifconfig_{nic}="DHCP"\n')
61-
os.system(f'/etc/pccard_ether {nic} startchildren')
63+
Popen('/etc/pccard_ether {nic} startchildren', shell=True)
64+
else:
65+
Popen(
66+
f'service netif start {nic} ; '
67+
f'service dhclient start {nic} ; '
68+
f'service routing restart',
69+
shell=True
70+
)

0 commit comments

Comments
 (0)