Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-02-2008, 08:02 AM   #1
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
script to update /etc/hosts on dhcp obtained ip change.

Hi I had some troubles at Sun java systema application server start process when my dhcp server gave me a diferent ip address.. so here is my solution:

bash-3.1# cat /usr/local/bin/

ifconfig | grep inet\ addr | grep -v '' |awk -F ' +||:+' '{print $4}' > /usr/local/bin/localhosts-addresses
grep localhost /etc/hosts | grep -v ''  | grep -v "#"| awk -F' +' '{print $1}'>/usr/local/bin/etclocalhosts-addresses
lineas=`wc -l /usr/local/bin/localhosts-addresses| cut -d' ' -f1`
lineasetclocalhosts=`wc -l /usr/local/bin/etclocalhosts-addresses| cut -d' ' -f1`
for i in `seq 1 $lineas` ;do
    ip=`head -n $i /usr/local/bin/localhosts-addresses | tail -1`
    if [ $i -le $lineasetclocalhosts ]; then
        ipetclocalhost=`head -n $i /usr/local/bin/etclocalhosts-addresses | tail -1`
        replace  "$ipetclocalhost" "$ip" -- /etc/hosts
        ip=`head -n $i /usr/local/bin/localhosts-addresses | tail -1`
        echo "$ip       localhost" >> /etc/hosts

also I did some changes to tha way I update my ip, previously i used the command dhcpcd <interface>. now I use a script that calls it and then calls my scrypt:
bash-3.1# cat /sbin/dhcp

/sbin/dhcpcd $1 -k
rm /etc/dhcpc/dhcpcd-$
/sbin/dhcpcd $1
hope It helps someone.
Old 02-02-2008, 10:17 AM   #2
LQ Guru
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 64
Very nice. I hate when apps expect to always have the same IP and rely on things like the hosts to be correct. Again very nice simply fix.



dhcp, dns, hosts

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How To know if a host configured with DHCP has obtained IP address from DHCP server? Prassanta SUSE / openSUSE 1 06-29-2006 10:48 AM
Hosts file entry with DHCP hutyerah Slackware 7 02-04-2006 05:13 AM
IP Address for /etc/hosts when using DHCP jimbo Linux - Networking 5 03-27-2004 07:59 PM
/etc/hosts (or: getting DHCP clients) billlee Linux - Networking 5 06-09-2003 04:42 PM
script to update /etc/hosts bradut Linux - Newbie 8 07-16-2002 11:52 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration