From 607e700855ab2701a5011696b7586e4e3fe0ddc6 Mon Sep 17 00:00:00 2001 From: ericbsd Date: Wed, 1 Sep 2021 08:29:31 -0300 Subject: [PATCH 1/3] Moved the code of stoping device netif if we find a card with network --- setup.py | 2 +- src/auto-switch.py | 13 ++++++------- src/net_api.py | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index 27e84ee..eb5a11a 100755 --- a/setup.py +++ b/setup.py @@ -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 diff --git a/src/auto-switch.py b/src/auto-switch.py index 02375a1..de0f3f6 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -25,7 +25,7 @@ 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() @@ -33,12 +33,6 @@ 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], @@ -48,6 +42,11 @@ ) nic_ifconfig = output.stdout.read() if 'status: active' in nic_ifconfig or 'status: associated' in nic_ifconfig: + if openrc: + os.system(f'service dhcpcd.{nic} stop') + else: + if nic in defautl_nic and nic_ifconfig not in defautl_nic: + os.system(f'service netif stop {nic}') if 'inet ' in nic_ifconfig or 'inet6' in nic_ifconfig: if openrc: os.system(f'service dhcpcd.{current_nic} restart') diff --git a/src/net_api.py b/src/net_api.py index 19d5055..fe682a6 100755 --- a/src/net_api.py +++ b/src/net_api.py @@ -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): From 6b77a8ab14a6d3aafb732466e04695af7d10e80b Mon Sep 17 00:00:00 2001 From: ericbsd Date: Fri, 3 Sep 2021 07:37:04 -0400 Subject: [PATCH 2/3] only stop dhcient if status is not active --- src/auto-switch.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/auto-switch.py b/src/auto-switch.py index de0f3f6..82049b9 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -14,6 +14,27 @@ 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: + os.system(f'service dhclient stop {nic}') + nics = Popen( ['ifconfig', '-l', 'ether'], stdout=PIPE, @@ -30,9 +51,6 @@ if not nic_list: exit() -cmd = 'netstat -rn | grep default' -defautl_nic = Popen(cmd, stdout=PIPE, shell=True, universal_newlines=True).stdout.read() - for current_nic in nic_list: output = Popen( ['ifconfig', current_nic], @@ -42,11 +60,6 @@ ) nic_ifconfig = output.stdout.read() if 'status: active' in nic_ifconfig or 'status: associated' in nic_ifconfig: - if openrc: - os.system(f'service dhcpcd.{nic} stop') - else: - if nic in defautl_nic and nic_ifconfig not in defautl_nic: - os.system(f'service netif stop {nic}') if 'inet ' in nic_ifconfig or 'inet6' in nic_ifconfig: if openrc: os.system(f'service dhcpcd.{current_nic} restart') From 1995bddf184a0120b4431e8605b5a333fa5ef598 Mon Sep 17 00:00:00 2001 From: ericbsd Date: Sat, 4 Sep 2021 19:10:58 -0300 Subject: [PATCH 3/3] Fix the auto switch to wlan when wire is unpluged --- src/auto-switch.py | 6 +++++- src/trayicon.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/auto-switch.py b/src/auto-switch.py index 82049b9..860577b 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -33,7 +33,11 @@ if openrc: os.system(f'service dhcpcd.{nic} stop') else: - os.system(f'service dhclient stop {nic}') + 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'], diff --git a/src/trayicon.py b/src/trayicon.py index 630628b..e9d0f16 100755 --- a/src/trayicon.py +++ b/src/trayicon.py @@ -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":