Quantcast
Channel: FedoraForum.org
Viewing all articles
Browse latest Browse all 36189

How I solved my DHCP roulette problem

$
0
0
I'm running three F17 systems on my LAN and I use NFS mounts on each as destinations for backup of the others. I had been manually maintaining the /etc/hosts files whenever a power outage resulted in a change to the addresses assigned by DHCP in my WRT4GL router. Well, the last set of storms and resultant outages pushed me to find a better solution so I wrote a bash shell script to use wget to talk to the router and get the current assignments for the three servers and create sed commands to adjust /etc/hosts when necessary. I run the script out of crontab every five minutes as root. Everyone is welcome to use and improve.

Code:

#!/bin/bash
#
# This script gets the currently assigned IP addresses for the LAN's desktop machines
# and updates /etc/hosts if necessary.
#

wget --http-user=admin --http-password=<your password here> --output-document=- http://192.168.1.1/DHCPTable.asp 2>/dev/null |grep -i -e <host1> -e <host2> -e <host3> |tr -d '^,' |tr "'" " " |tr '[A-Z]' '[a-z]' |awk '{print $1, $2}' |sort > /tmp/check_ip_current.txt

cat /dev/null > /tmp/check_ip_hosts.sed

while read HOST_NAME CURRENT_IP_ADDRESS
do
        IP_ADDRESS=`grep -i "$HOST_NAME" /etc/hosts |grep -v '^#' |awk '{print $1}'`
       
        if [ "$CURRENT_IP_ADDRESS" != "$IP_ADDRESS" ]
        then
                echo "s~${IP_ADDRESS}~${CURRENT_IP_ADDRESS}~g" |tr '~' '/' >> /tmp/check_ip_hosts.sed
        fi
done < /tmp/check_ip_current.txt

if [ -s /tmp/check_ip_hosts.sed ]
then
        sed -f /tmp/check_ip_hosts.sed /etc/hosts > /tmp/check_ip_hosts.txt
        cp /etc/hosts /etc/hosts_prev
        cat /tmp/check_ip_hosts.txt > /etc/hosts
        diff /etc/hosts_prev /etc/hosts > /tmp/check_ip_email.txt
        mailx -s "check_ip.bash $HOSTNAME" terrancemccann@gmail.com < /tmp/check_ip_email.txt
fi

Terry

Viewing all articles
Browse latest Browse all 36189

Trending Articles