Skip to content

Instantly share code, notes, and snippets.

@glennschler
Created June 10, 2012 18:53
Show Gist options
  • Save glennschler/2906911 to your computer and use it in GitHub Desktop.
Save glennschler/2906911 to your computer and use it in GitHub Desktop.
Removes old entries of local hostname from the hosts file since the REH EC2 image inserts a new entry every reboot
#! /bin/sh -x
#
# chkconfig: 2345 96 99
# description: removes old entries for this server name from the host file
# add then adds the correct hosts entry
#
hname=`hostname`
lev=`who -r`
echo "Called with ${1} hostname=${hname} ${lev}" | logger -t 'ora-ec2hosts'
case $1 in
'start')
#get the real local ip
ip="`/sbin/ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'`"
#get the the ip of where the hostname (host file) resolves
ping -c 1 -w 1 `hostname` | grep "PING" > /tmp/hostip.txt
hostip=`sed "s/PING.* (\([^)]*\).*//" < /tmp/hostip.txt`
echo "real=${ip} hostsetto=${hostip}" | logger -t 'ora-ec2hosts'
# compare and replace if not the same
if [ "${ip}" != "${hostip}" ]
then
# first delete the old entries
cp -u /etc/hosts /etc/hosts.bak
sed "/$hname/d" /etc/hosts > /tmp/sedr1.txt
sed "/$ip/d" /tmp/sedr1.txt > /etc/hosts
# replace
echo "${ip} ${hname}" >> /etc/hosts
echo "now ${hname} set to =${ip}" | logger -t 'ora-ec2hosts'
fi
# so the backup is never overwritten again
touch /etc/hosts.bak
;;
*)
echo 'usage: $1 {start}'
exit
;;
esac
#
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment