Skip to content

Instantly share code, notes, and snippets.

@sophieforceno
Last active December 11, 2021 23:40
Show Gist options
  • Save sophieforceno/bf137b32759ea5be8b91 to your computer and use it in GitHub Desktop.
Save sophieforceno/bf137b32759ea5be8b91 to your computer and use it in GitHub Desktop.
A Bash script that enables/disables network adapters depending on connectivity
#! /bin/bash
# net-adapt v0.9 (build 110915)
#
# Enables and disables WIFI device depending on ethernet connectivity
# by Sophie Forceno
#
# Distributed under an "I-don't-care-what-you-do-with-this" license
while true
do
eth0_status=$(cat /sys/class/net/eth0/operstate)
wlan0_status=$(cat /sys/class/net/wlan0/operstate)
# Disable wifi when cable is plugged in
if [[ "$eth0_status" = "up" ]]; then
nmcli nm wifi off
# Enable wifi when ethernet cable is unplugged
elif [[ "$eth0_status" = "down" ]] && [[ "$wlan0_status" = "down" ]]; then
nmcli nm wifi on
fi
sleep 2s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment