n8felton
7/9/2013 - 7:42 PM

Check to see if you can ping the Gateway IP address, and if not, reset the interface.

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