[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Updating /etc/hosts automatically / behavior of sed command



Hi !

I have a cable connection, but with a dynamic IP adress.

Fed up with changing my /etc/hosts file by hand each time my IP adress changes
(my ISP's server isn't totally stable, and sometimes my IP changes even if I
don't reboot), I wrote a little bash script which checks if the IP adress has
changed, and then modifies automatically the file /etc/hosts.

So far, so good. But I wrote another script that uses the sed command as well,
and the behavior of that one isn't always stable (it sometimes produce an
empty file, whereas it should have produced something).

So here's the point. I would like someone who knows the sed command well enough
to tell me if my script can ever mess up with the /etc/hosts file.

I added a little security (if the file produced is empty, it does not replace
the /etc/hosts with it) but that still annoys me. What if the file wouldn't be
empty, but with lines erased ?

Of course, anyone who is interested in this script can copy it and use it.

Personnally, I put it in the startup scripts (in /etc/init.d) and defined a
cron job which launch it every 15 minutes.

And... If it works well, why about including it in the future Debian ? :-)

Any help would be appreciated.

Thanks !

-- 
Raphaël HALIMI
#! /bin/sh

# Modify your file /etc/hosts if the IP adress has changed after a reboot
# By Raphaël HALIMI <RaphaelH@Cybercable.Fr>
# Last modification: December 24th 1999 at 17:34 (Merry Christmas ! :-)
 
INTERFACE=eth0

# First we check if the interface is configured

if ! /sbin/ifconfig $INTERFACE > /dev/null 2>&1 ; then
   echo "Network interface $INTERFACE not configured: aborting."
   exit
fi

# Definitions of old and new the IP adresses

CURRENT_IP=`/sbin/ifconfig $INTERFACE | grep inet | cut -d ":" -f 2 | cut -d " " -f 1`
REGISTERED_IP=`grep $HOSTNAME /etc/hosts | cut -f 1`

echo "Current IP: $CURRENT_IP."
echo "Registered IP: $REGISTERED_IP."

# Check if IP is modified, and if so, we update the /etc/hosts file
# If the creation of the new /etc/hosts file failed, no modification is done

if [ $CURRENT_IP != $REGISTERED_IP ] ; then
   echo -n "IP adress has changed: créating a new /etc/hosts file"
   sed -e "s/$REGISTERED_IP/$CURRENT_IP/g" /etc/hosts > /etc/hosts.new
   echo "."
   if [ -s /etc/hosts.new ] ; then
      echo -n "Creation of the new file succeeded: replacing /etc/hosts"
      cp /etc/hosts /etc/hosts.bak
      mv /etc/hosts.new /etc/hosts
      echo "."
      echo "Backup copy of the old /etc/hosts: /etc/hosts.bak."
   else
      echo "Error when creating file: /etc/hosts.new is empty, no update done."
      echo "Please modify /etc/hosts by yourself."
   fi
else
   echo "IP adress hasn't changed: no update needed."
fi

# Comment out these lines if you don't want to be informed if your IP adress has
# changed (I like to know what's going on my machine :-)
# You can add people you want to be informed of the change in the $INFORMED
# variable, just add the names (or e-mails) seperated by commas

INFORMED="root"

if [ $CURRENT_IP != $REGISTERED_IP ] ; then
   echo "New IP adress: $CURRENT_IP" | mail $INFORMED -s "IP adress of $HOSTNAME has changed"
   echo "A mail has been sent to $INFORMED."
fi

Reply to: