Skip to content

Instantly share code, notes, and snippets.

@dz0ny
Last active January 13, 2022 10:15
Show Gist options
  • Save dz0ny/b90bcca9d60f6e8a2a33c55132d68c52 to your computer and use it in GitHub Desktop.
Save dz0ny/b90bcca9d60f6e8a2a33c55132d68c52 to your computer and use it in GitHub Desktop.
#!/bin/bash
destination_host=$(/sbin/ip route | awk '/default/ { print $3 }')
if [[ $destination_host != "" ]]; then # check if we have default route (connected to router)
echo "Default gateway found $destination_host ..."
ping -c4 $destination_host >/dev/null # check if destination is reachable - possible by default the router
if [ $? != 0 ]; then
echo "Destination not reachable - reconfigure interface..."
sudo wpa_cli -i wlan0 reconfigure # reconfigure network to trigger reconnect
sudo dhclient -v # ensure connection will be established by refresh dhcp lease
echo "Reconnect done."
exit 0 # if we detect multiple network configs exit after 1st one - one reconnect is enough :-)
else
echo "Destination reachable - no action needed."
exit 0 # destination reached - exit loop
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment