Skip to content

Commit

Permalink
Merge pull request #75 from ghostbsd/setup-autoswitch-bug
Browse files Browse the repository at this point in the history
Fixed auto switch to only stop deivce if status is not active
  • Loading branch information
ericbsd authored Sep 4, 2021
2 parents 3ab14f8 + 1995bdd commit 0dee331
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from setuptools import setup
from subprocess import run

__VERSION__ = '6.0'
__VERSION__ = '6.1'
PROGRAM_VERSION = __VERSION__

prefix = '/usr/local' if system() == 'FreeBSD' else sys.prefix
Expand Down
36 changes: 26 additions & 10 deletions src/auto-switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@
rc_system = Popen(cmd, stdout=PIPE, universal_newlines=True).stdout.read()
openrc = True if 'openrc' in rc_system else False

cmd = 'netstat -rn | grep default'
defautl_nic = Popen(cmd, stdout=PIPE, shell=True, universal_newlines=True).stdout.read()

nic_ifconfig = Popen(
['ifconfig', nic],
stdout=PIPE,
close_fds=True,
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 any(active_status) is False:
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,
Expand All @@ -25,20 +50,11 @@
r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)"

nics_lelfover = nics.stdout.read().replace(nic, '').strip()
nic_list = re.sub(notnics_regex, '', nics_lelfover).strip().split()
nic_list = sorted(re.sub(notnics_regex, '', nics_lelfover).strip().split())

if not nic_list:
exit()

cmd = 'netstat -rn | grep default'
defautl_nic = Popen(cmd, stdout=PIPE, shell=True, universal_newlines=True).stdout.read()

if openrc:
os.system(f'service dhcpcd.{nic} stop')
else:
if nic in defautl_nic and 'wlan' not in defautl_nic:
os.system(f'service netif stop {nic}')

for current_nic in nic_list:
output = Popen(
['ifconfig', current_nic],
Expand Down
2 changes: 1 addition & 1 deletion src/net_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def nics_list():
stdout=PIPE,
universal_newlines=True
).stdout.read().strip()
return re.sub(notnics_regex, '', nics).strip().split()
return sorted(re.sub(notnics_regex, '', nics).strip().split())


def ifcardconnected(netcard):
Expand Down
2 changes: 1 addition & 1 deletion src/trayicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def nm_menu(self):
disconnected = Gtk.MenuItem("Wired %s Unplug" % cardnum)
disconnected.set_sensitive(False)
self.menu.append(disconnected)
cardnum += 1
cardnum += 1
self.menu.append(Gtk.SeparatorMenuItem())
elif "wlan" in netcard:
if connection_state == "Disabled":
Expand Down

0 comments on commit 0dee331

Please sign in to comment.