Skip to content

Instantly share code, notes, and snippets.

@rosston
Last active October 28, 2021 23:52
Show Gist options
  • Save rosston/2b114442bcdc8ad2fea09aabe3c9e19d to your computer and use it in GitHub Desktop.
Save rosston/2b114442bcdc8ad2fea09aabe3c9e19d to your computer and use it in GitHub Desktop.
A script to check which of your network interfaces is up
#!/bin/bash
# Built only to support macOS, almost certainly doesn't work on Linux
lan_mac='<your LAN MAC address>'
wifi_mac='<your Wi-Fi MAC address>'
test_url='<some test URL, which should fully support both IPv4 and IPv6>'
interfaces=$(ifconfig | grep -E '^[A-z0-9]+: ' | sed 's/: .*$//g')
reset=0
if [ "$1" == "--reset" ]; then
reset=1
fi
matches_mac() {
interface=$1
mac=$2
ifconfig "$interface" | grep 'ether' | grep "$mac" > /dev/null
}
check_url() {
interface=$1
ipv=$2
curl --interface "$interface" $ipv -m 2 -I -s "$test_url" > /dev/null
if [ $? -eq 0 ]; then
echo "UP"
else
echo "DOWN"
fi
}
reset_interface() {
interface=$1
sudo ifconfig "$interface" down
sudo ifconfig "$interface" up
}
lan_interface=''
wifi_interface=''
for interface in $interfaces; do
if matches_mac "$interface" "$lan_mac"; then
lan_interface=$interface
fi
if matches_mac "$interface" "$wifi_mac"; then
wifi_interface=$interface
fi
done
if [ ! -z "$lan_interface" ]; then
if [ $reset -eq 1 ]; then
echo "Resetting LAN interface..."
reset_interface "$lan_interface"
echo "Done"
else
echo ""
echo "LAN status:"
echo " IPv4: $(check_url "$lan_interface" "-4")"
echo " IPv6: $(check_url "$lan_interface" "-6")"
fi
fi
if [ ! -z "$wifi_interface" ]; then
if [ $reset -eq 1 ]; then
echo "Resetting Wi-Fi interface..."
reset_interface "$wifi_interface"
echo "Done"
else
echo ""
echo "Wi-Fi status:"
echo " IPv4: $(check_url "$wifi_interface" "-4")"
echo " IPv6: $(check_url "$wifi_interface" "-6")"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment