LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2002, 07:11 PM   #1
bradut
LQ Newbie
 
Registered: Dec 2001
Location: London
Distribution: Debian Woody
Posts: 17

Rep: Reputation: 0
script to update /etc/hosts


Please let me first apologise if this is the wrong place to post this, but I am not a regular on this forum so this seemed a good place to me.

I have a machine connected via ADSL with a dynamic ip address. I want to include a little scriplet which will update my dynamic ip address in /etc/hosts.
Right now my /etc/host has this entry: 127.0.0.1 localhost.localdomain localhost
I want the scriptlet (which I was thinking to include it as part of /etc/ppp/ip-up ) to add a second line containing ipaddress hostname.mydomain hostname.
I found this so far, which retrieves my dynamic ip address:
exec /sbin/ifconfig ppp0 | grep 'inet addr' | awk '{print $2}' | sed - e "s/.*://
which if I execute in shell will give me the ip address. Could someone help me finish this off?

Thanks a lot

PS I promise that I will study the vi, sed....etc manuals in detail, but for now I just need this script to work..... please..!!! ..
 
Old 07-15-2002, 07:55 PM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Quick and dirty hint - use echo
echo "`exec /sbin/ifconfig ppp0 | grep 'inet addr' | awk '{print $2}' | sed -e s/.*://` hostname domain" >> /etc/hosts
 
Old 07-15-2002, 07:58 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
but then of course you'll screw up the file if that runs more than once.... either keep a backup copy that you can copy to /etc/hosts and append each time or similar
 
Old 07-15-2002, 08:01 PM   #4
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Yeah that's true, it only runs once, for the second time it will screw up the file, thanks acid for pointing it out
 
Old 07-15-2002, 08:05 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
no problem. any petty detail, no matter how pointless... i'm your man.
 
Old 07-15-2002, 08:15 PM   #6
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
heads up, acid . I just thought of a quick solution and it would be to delete /etc/hosts and then create it
Code:
#!/bin/sh
rm -f /etc/hosts
echo "127.0.0.1 localhost localdomain">/etc/hosts
echo "`exec /sbin/ifconfig ppp0 | grep 'inet addr' | awk '{print $2}' | sed -e s/.*://` hostname domain" >> /etc/hosts
 
Old 07-15-2002, 09:15 PM   #7
bradut
LQ Newbie
 
Registered: Dec 2001
Location: London
Distribution: Debian Woody
Posts: 17

Original Poster
Rep: Reputation: 0
thanks guys, this did the trick. I still can't get aolserver to see the new ipaddress and hostname, but this is a different matter altogether.

all the best
 
Old 07-16-2002, 03:16 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
ahh but then you're deleting the file and then overwriting it regardless.... remove the rm and you'd never know the difference
 
Old 07-16-2002, 10:52 AM   #9
bradut
LQ Newbie
 
Registered: Dec 2001
Location: London
Distribution: Debian Woody
Posts: 17

Original Poster
Rep: Reputation: 0
thanks a lot

All done, aolserver serving what is supposed to serve on the right port, script works a treat.

Thank you once again
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Update Script justin_p Slackware 2 06-06-2005 10:56 AM
Dependency update script? Jalalabee Linux - Newbie 1 12-18-2003 06:03 AM
update script from a file knobby Linux - General 7 01-11-2002 11:01 AM
script to update my webpage yank_ Linux - General 0 02-16-2001 05:56 PM


All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration