Skip to content

Instantly share code, notes, and snippets.

@n8felton
Created July 9, 2013 19:42
Show Gist options
  • Save n8felton/5960598 to your computer and use it in GitHub Desktop.
Save n8felton/5960598 to your computer and use it in GitHub Desktop.
Check to see if you can ping the Gateway IP address, and if not, reset the interface.
#!/bin/bash
# Check to see if you can ping the Gateway IP address, and if not, reset the interface.
# Author: Nathan Felton
# Date: 2013-07-09
if [ $EUID -ne 0 ]; then
echo "Run as root."
exit 1
fi
GATEWAY=$(netstat -rn | grep "default" | awk '{print $2}')
if [[ $GATEWAY =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
INTERFACE=$(netstat -rn | grep "default" | awk '{print $6}')
PING=$(ping -c 1 $GATEWAY)
if [ $? -eq 2 ]; then
echo "Gateway ping failed. Resetting $INTERFACE"
ifconfig $INTERFACE down
ifconfig $INTERFACE up
fi
else
echo "Network not active or is misconfigured."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment