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..860577b 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -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, @@ -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], 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): 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":