Skip to content

Commit

Permalink
only stop dhcient if status is not active
Browse files Browse the repository at this point in the history
  • Loading branch information
ericbsd committed Sep 3, 2021
1 parent 607e700 commit 6b77a8a
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/auto-switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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],
Expand All @@ -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')
Expand Down

0 comments on commit 6b77a8a

Please sign in to comment.