LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to update /etc/hosts (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-update-etc-hosts-25831/)

bradut 07-15-2002 07:11 PM

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..!!! ..

neo77777 07-15-2002 07:55 PM

Quick and dirty hint - use echo
echo "`exec /sbin/ifconfig ppp0 | grep 'inet addr' | awk '{print $2}' | sed -e s/.*://` hostname domain" >> /etc/hosts

acid_kewpie 07-15-2002 07:58 PM

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

neo77777 07-15-2002 08:01 PM

Yeah that's true, it only runs once, for the second time it will screw up the file, thanks acid for pointing it out

acid_kewpie 07-15-2002 08:05 PM

no problem. any petty detail, no matter how pointless... i'm your man. :)

neo77777 07-15-2002 08:15 PM

heads up, acid :p . 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


bradut 07-15-2002 09:15 PM

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

acid_kewpie 07-16-2002 03:16 AM

ahh but then you're deleting the file and then overwriting it regardless.... remove the rm and you'd never know the difference

bradut 07-16-2002 10:52 AM

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

Thank you once again


All times are GMT -5. The time now is 10:22 AM.