LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-02-2008, 08:02 AM   #1
juanctes
Member
 
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:

Code:
bash-3.1# cat /usr/local/bin/hosts.sh
#!/bin/bash

ifconfig | grep inet\ addr | grep -v '127.0.0.1' |awk -F ' +||:+' '{print $4}' > /usr/local/bin/localhosts-addresses
grep localhost /etc/hosts | grep -v '127.0.0.1'  | 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
    else
        ip=`head -n $i /usr/local/bin/localhosts-addresses | tail -1`
        echo "$ip       localhost" >> /etc/hosts

    fi
done
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 hosts.sh scrypt:
Code:
bash-3.1# cat /sbin/dhcp
#!/bin/bash

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

Rep: Reputation: 61
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.

Brian
 
  


Reply

Tags
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/Novell 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


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

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