#!/bin/bash # # 01-01 Pete Nesbitt # problem with interface 0, loses IP address periodicly # script to check eth0 is up, then for a valid IP. # # NOTE: I also edited network script to restart firewall. # Otherwise need to start ipchains after calls to start network within this script. # already have a "emailnotice" alias in /etc/aliases DATE=`date` # set variables for email to false ALERT=0 LALERT=0 # another file holds the previous IP OLDIP="/root/cronjobs/oldip" # check that the link is up, if it is up, the following will be a non-empty file LSTAT="`/sbin/ifconfig -a|grep -A 3 eth0|grep UP`" if [ -z "$LSTAT" ]; then while [ -z "$LSTAT" ] do /etc/rc.d/init.d/network restart LSTAT="`/sbin/ifconfig -a|grep -A 3 eth0|grep UP`" LALERT=`echo "$LALERT + 1" | bc` done echo "The external interface was DOWN, it was restarted at $DATE after $LALERT tries."|mail -s "Restarted Interface" emailnotice fi # check for valid IP address in ifconfig # if line 2 has the string adr, then there will be an IP STATUS="`/sbin/ifconfig -a|grep -A 2 eth0|grep inet`" if [ -z "$STATUS" ]; then while [ -z "$STATUS" ] do /etc/rc.d/init.d/network restart STATUS="`/sbin/ifconfig -a|grep -A 2 eth0|grep inet`" ALERT=`echo "$ALERT + 1" | bc` NEWIP="`/sbin/ifconfig -a|grep -A 2 eth0|awk '/inet/ { print $2 }'|sed -e s/addr://`" done echo "The IP was reset at $DATE after $ALERT times, the new IP address is $NEWIP the last IP was `cat $OLDIP`"|mail -s "Lost IP" emailnotice echo "$NEWIP">$OLDIP fi exit