-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from ghostbsd/issue-64
Improvement for switching nic when dettaching or attaching nics
- Loading branch information
Showing
6 changed files
with
126 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
import os | ||
import re | ||
from subprocess import Popen, PIPE | ||
|
||
args = sys.argv | ||
if len(args) != 2: | ||
exit() | ||
nic = args[1] | ||
|
||
cmd = ["kenv", "rc_system"] | ||
rc_system = Popen(cmd, stdout=PIPE, universal_newlines=True).stdout.read() | ||
openrc = True if 'openrc' in rc_system else False | ||
|
||
if openrc: | ||
os.system(f'service dhcpcd.{nic} stop') | ||
|
||
nics = Popen( | ||
['ifconfig', '-l'], | ||
stdout=PIPE, | ||
close_fds=True, | ||
universal_newlines=True | ||
) | ||
nics = nics.stdout.read().strip() | ||
|
||
notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ | ||
r"ppp|bridge|ixautomation|vm-ixautomation|wg)[0-9]+(\s*)" | ||
|
||
recompile = re.compile(notnics_regex) | ||
nics = recompile.sub('', nics).replace(nic, '').strip(). | ||
|
||
if not nics: | ||
exit() | ||
|
||
nic_list = nics.split() | ||
|
||
for card in nic_list: | ||
print(card) | ||
ncard = 'ifconfig -l' | ||
output = Popen( | ||
['ifconfig', card], | ||
stdout=PIPE, | ||
close_fds=True, | ||
universal_newlines=True | ||
) | ||
nic_ifconfig = output.stdout.read() | ||
if 'status: active' in nic_ifconfig or 'status: associated' in nic_ifconfig: | ||
if 'inet ' in nic_ifconfig or 'inet6' in nic_ifconfig: | ||
if openrc: | ||
os.system(f'service dhcpcd.{card} restart') | ||
else: | ||
os.system(f'service dhclient restart {card}') | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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_DOWN"; | ||
|
||
action "/usr/local/share/networkmgr/auto-switch.py $subsystem"; | ||
}; | ||
|
||
attach 100 { | ||
device-name "$wifi-driver-regex"; | ||
action "/usr/local/share/networkmgr/setup-nic.py $device-name"; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import re | ||
import sys | ||
|
||
args = sys.argv | ||
if len(args) != 2: | ||
exit(1) | ||
nic = args[1] | ||
|
||
rcconf = open('/etc/rc.conf', 'r').read() | ||
if os.path.exists('/etc/rc.conf.local'): | ||
rcconflocal = open('/etc/rc.conf.local', 'r').read() | ||
else: | ||
rcconflocal = "None" | ||
|
||
notnics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ | ||
"ppp|bridge|ixautomation|vm-ixautomation|wg|wlan)[0-9]+" | ||
|
||
# wifi_driver_regex is taken from devd.conf wifi-driver-regex | ||
wifi_driver_regex = "(ath|bwi|bwn|ipw|iwi|iwm|iwn|malo|mwl|otus|ral|rsu|" \ | ||
"rtwn|rum|run|uath|upgt|ural|urtw|wi|wpi|wtap|zyd)[0-9]+" | ||
|
||
if re.search(notnics_regex, nic): | ||
exit(0) | ||
if re.search(wifi_driver_regex, nic): | ||
if not os.path.exists('/etc/wpa_supplicant.conf'): | ||
open('/etc/wpa_supplicant.conf', 'a').close() | ||
os.system('chown root:wheel /etc/wpa_supplicant.conf') | ||
os.system('chmod 765 /etc/wpa_supplicant.conf') | ||
for wlanNum in range(0, 9): | ||
if f'wlan{wlanNum}' not in (rcconf + rcconflocal): | ||
break | ||
if f'wlans_{nic}=' not in (rcconf + rcconflocal): | ||
rc = open('/etc/rc.conf', 'a') | ||
rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n') | ||
rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n') | ||
rc.close() | ||
else: | ||
if f'ifconfig_{nic}=' not in (rcconf + rcconflocal): | ||
rc = open('/etc/rc.conf', 'a') | ||
rc.writelines(f'ifconfig_{nic}="DHCP"\n') | ||
rc.close() | ||
os.system(f'/etc/pccard_ether {nic} startchildren') |
This file was deleted.
Oops, something went wrong.