#!/bin/bash

NETMASK="255.255.255.0"
INTERFACE="eth0"

function bringup()
{
        find /etc/sysconfig/network-scripts/ -name "ifcfg-$INTERFACE:*" -print0|xargs -0 -n 1 echo | awk -F"/" '{ print $5 }' | awk -F"-" '{ print $2 }' | while read ifcfg_eth; do

                IPADDR=`grep -Ei "IPADDR=" /etc/sysconfig/network-scripts/ifcfg-$ifcfg_eth | awk -F"=" '{ print $2 }'`

                #ifconfig $ifcfg_eth down
                #echo "ifconfig $ifcfg_eth down"
                #if [[ `grep -ci "NETMASK=" /etc/sysconfig/network-scripts/ifcfg-$ifcfg_eth` == 1 ]]; then
                #        NETMASK=`grep -Ei "NETMASK=" /etc/sysconfig/network-scripts/ifcfg-$ifcfg_eth | awk -F"=" '{ print $2 }'`
                #        echo "NETMASK: $NETMASK was found"
                #else
                #        echo "no netmask found, we use default defined one"
                #fi

                echo "ifcfg_eth: $ifcfg_eth with IPADDR: $IPADDR"
                ifconfig $ifcfg_eth inet $IPADDR netmask $NETMASK
        done
}

function repair_the_mess()
{
        find /etc/sysconfig/network-scripts/ -name "ifcfg-$INTERFACE:*" -print0|xargs -0 -n 1 echo | awk -F"/" '{ print $5 }' | awk -F"-" '{ print $2 }' | while read ifcfg_eth; do

                IPADDR=`grep -Ei "IPADDR=" /etc/sysconfig/network-scripts/ifcfg-$ifcfg_eth | awk -F"=" '{ print $2 }'`

                if [[ `ping $IPADDR -c 1 | grep "nexthop"` ]]; then
                        echo "we found dead IP with: $IPADDR"
                        ifconfig $ifcfg_eth down
                        ifconfig $ifcfg_eth inet $IPADDR netmask $NETMASK
                fi
        done
}

bringup
repair_the_mess
