LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Network repair script (https://www.linuxquestions.org/questions/linux-networking-3/network-repair-script-721312/)

vendetta007 04-23-2009 11:11 PM

Network repair script
 
I was having problems with my modem resetting and causing interesting issues with my firewall / routing tables / and NIC status so I built this script. I thought I would post it up so you guys could either help me refine it, use it yourselves, or tell me that something better already exists.

Enjoy

Code:

#!/bin/bash

##################################################################
## Debug Toggle, enter any value to turn on debugging.

DEBUG=''

##################################################################
## Configuration and variable definition.

## The utilities we use are defined here.
CUT="/bin/cut"
EXPR="/usr/bin/expr"
FIREWALL_INIT="/etc/init.d/init.firewall"
GREP="/bin/grep"
IFCONFIG="/sbin/ifconfig"
IFDOWN="/sbin/ifdown"
IFUP="/sbin/ifup"
IPTABLES="/sbin/iptables"
PING="/bin/ping"
ROUTE="/sbin/route"
SERVICE="/sbin/service"
WC="/usr/bin/wc"

## Our resets are initialized here
FIREWALL="N"
GATEWAY="N"
NETWORK="N"

## Our network parameters are defined and gathered here
EXTERNAL="eth0"
INTERNAL="eth1"

CURRENT=`$ROUTE -n | $GREP ^0.0.0.0 | $CUT -d \  -f 10`
#        CURRENT=`cat /root/test-route | $GREP ^0.0.0.0 | $CUT -d \  -f 10`

EXT_IP=`$IFCONFIG $EXTERNAL | $GREP inet | $CUT -d : -f 2 | $CUT -d \  -f 1`
INT_IP=`$IFCONFIG $INTERNAL | $GREP inet | $CUT -d : -f 2 | $CUT -d \  -f 1`

NETADDR=`$IFCONFIG |
        while read w x y y; do
            if [ "$w" = "inet" ]; then
                set -- ${x//./ }; a=${1#addr:}; b=$2; c=$3; d=$4;
                set -- ${y//./ }; w=${1#Mask:}; x=$2; y=$3; z=$4;
                echo $((a&w)).$((b&x)).$((c&y)).$((d&z));
                break;
            fi;
        done`
TEMP=`echo $NETADDR | cut -d . -f 4`
EXT_GW=`echo -n ${NETADDR%$TEMP};((TEMP++));echo $TEMP`

# There are two firewall lengths depending upon whether
#  moblock is running or not.
EXPECTED_FW_LENGTH_1=147
EXPECTED_FW_LENGTH_2=166
FIREWALL_LENGTH=`$IPTABLES -L -n | $WC -l`


if [ $DEBUG ]; then
        echo "Calculated GW: " $EXT_GW
fi

test() {
        #########################################################
        ## Test for a full firewall

        if [ $FIREWALL_LENGTH != $EXPECTED_FW_LENGTH_1 ] && [ $FIREWALL_LENGTH != $EXPECTED_FW_LENGTH_2 ]; then
                if [ $DEBUG ]; then
                        echo "Tag Firewall for Reset";
                fi               

                FIREWALL="Y";
        fi

        PING_TEST=`$PING -c 1 4.2.2.1 | $GREP "1 received" | $WC -l`

        if [ $PING_TEST != 1 ]; then
                if [ $DEBUG ]; then
                        echo "Ping test failed";
                else
                        FIREWALL="Y";
                fi
        fi
       

        #########################################################
        ## Test for an invalid EXT_IP

        if [ $DEBUG ]; then
                echo "Current GW: " $CURRENT
                echo -n "External Int: " $EXTERNAL
                echo "  External IP: " $EXT_IP
                echo -n "Internal Int: " $INTERNAL
                echo "  Internal IP: " $INT_IP
        fi
       
        if [ -z $EXT_IP ]; then
                if [ $DEBUG ]; then
                        echo "No External IP"
                fi
                NETWORK="$EXTERNAL"
        fi

        if [ -z $INT_IP ]; then
                if [ $DEBUG ]; then
                        echo "No Internal IP"
                fi
                NETWORK="$INTERNAL"
        fi

        #########################################################
        ## Test for an invalid default route and repair it.

        if [[ $CURRENT == 192.168.* ]]; then
                if [ $DEBUG ] ; then
                        echo "Class C Private Network";
                fi
                GATEWAY="Y";
        elif [[ $CURRENT =~ 172.* ]]; then
                SECOND=`$EXPR "$CURRENT" : '\(...\.[0-9]*\)' | $CUT -d . -f 2`;
                if [ $SECOND -ge 16 -a $SECOND -le 32 ]; then
                        if [ $DEBUG ] ; then
                                echo "Class B Private Network";
                        fi
                        GATEWAY="Y";
                fi       
        elif [[ $CURRENT == 10.* ]]; then
                if [ $DEBUG ]; then
                        echo "Class A Private Network";
                fi
                        GATEWAY="Y";
        elif [ -z $CURRENT ]; then
                if [ $DEBUG ]; then
                        echo "Null \$CURRENT value";
                fi
                NETWORK="all";
                FIREWALL="Y";
        elif [ $CURRENT = $INT_IP ]; then
                if [ $DEBUG ]; then
                        echo "Routed internally";
                fi
                GATEWAY="Y";
        elif [ $CURRENT = $EXT_IP ]; then
                if [ $DEBUG ]; then
                        echo "Improper External Route";
                fi
                GATEWAY="Y"
        else
                if [ $DEBUG ] ; then
                        echo "Public Network";
                fi
        fi       
}

gateway() {
        if [ $DEBUG ]; then
                echo "Gateway Reset"
                if [ `expr length "$CURRENT"` -gt 15 ]; then
                        echo "Current gateway too long for single entry"
                fi
        else
                $ROUTE del -net 169.254.0.0 netmask 255.255.0.0 gw 0.0.0.0
                $ROUTE del -net 0.0.0.0 gw $INT_IP
                if [ `expr length "$CURRENT"` -le 15 ]; then
                        $ROUTE add -net 0.0.0.0 gw $EXT_GW
                fi;
        fi
}

firewall() {
        if [ $DEBUG ]; then
                echo "Init Firewall"
        else
                $FIREWALL_INIT 0
        fi
}

network() {
        if [[ $NETWORK -eq $EXTERNAL ]]; then
                if [ $DEBUG ]; then
                        echo "Reset Eth0";
                else
                        $SERVICE network restart;
                        sleep 10;
                fi
                GATEWAY="Y";
        elif [[ $NETWORK -eq $INTERNAL ]]; then
                if [ $DEBUG ]; then
                        echo "Reset Eth1";
                else
                        $IFCONFIG eth1 down;
                        $IFCONFIG eth1 up;
                fi
        elif [[ $NETWORK -eq "all" ]]; then
                if [ $DEBUG ]; then
                        echo "Reset all interfaces";
                else
                        $SERVICE network restart;
                fi
                GATEWAY="Y";
        else
                if [ $DEBUG ]; then
                        echo "No Match";
                fi
        fi
        FIREWALL="Y";
}

test

if [ $DEBUG ]; then
        echo -n "Test Status:  Network - " $NETWORK
        echo -n "  Gateway - " $GATEWAY
        echo "  Firewall - " $FIREWALL
fi

if [[ $NETWORK != 'N' ]]; then
        network
fi

if [[ $GATEWAY != 'N' ]]; then
        gateway
fi

if [[ $FIRWEALL != 'N' ]]; then
        firewall
fi



All times are GMT -5. The time now is 03:01 AM.