LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2009, 11:11 PM   #1
vendetta007
LQ Newbie
 
Registered: Apr 2007
Location: Los Angeles, CA
Distribution: Fedora Core 10
Posts: 16

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need a Linux OS other than Ubuntu which has a repair disk or repair software jhmac77 Linux - Newbie 1 03-17-2008 07:04 AM
repair grub loader over network is it possible? kissthis66 Linux - Newbie 4 02-15-2007 09:32 AM
can you repair grub over network? kissthis66 Linux - Laptop and Netbook 1 02-14-2007 03:24 PM
what is the use of network-script, resolv.conf, network nawuza Linux - Software 3 01-06-2007 08:23 AM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration