[BlueOnyx:10831] Re: question inre fixhosts (#2)

Larry Smith lesmith at ecsis.net
Mon Jun 18 21:41:04 PET 2012


On Mon June 18 2012 17:09, Michael Stauber wrote:
> Hi Larry,
>
> > (sending again since not sure if original made it)...
>
> It made it. Just haven't had the time to answer yet.
>
> > Just curious, but why was the "test" removed from the
> > /usr/sausalito/constructor/base/vsite/fixhosts file ???
> >
> > Previous it was testing to see if the IP was already in the file,
> >  (the cat /etc/hosts | grep $ip 2>/dev/null line)
> > which was followed by the test ( if $? != 0) and it added the line
> > in.  Now it just adds every IP the box has with the name of the
> > primary IP of the box itself everytime it runs and mucks up my
> > hostname fixes
> > (I edit the host file and fix the names so that sendmail answers
> > correctly).
>
> Well, the tests were only working right if you added a site. But on
> Vsite renames or deletes it would leave the old entries in, which could
> lead to undesireable results.
>
> Short of dumping the shell script and rewriting it properly in Perl I
> threw out the test as an interim measure. The file is now recreated from
> scratch every time around and all the existing Vsites are added to it.
> It's not perfect and in the longer run I will create a Perl script that
> does a better job, but what's there now is defenitely better than what
> we had before.

Appreciate the response, and it would be (or seem to me)
logical, except that the current script is simply using "hostname"
for all the IP addresses which puts the name of the box itself
instead of the correct name for the IP address....

This quick hack, along with what is already there, removes everything
between the begin Nuonce fix and end Nuonce fix lines, then adds back
in each IP that does not already exist in the file, with the hosts real name
if it exists, and the hostname of the box if it does not...  I figure if 
somone has manually modified the hosts file outside of the begin and end
Nuonce fix lines, they probably had a clue what they were doing (or thought
they did anyway) and there is no program way of covering for that...
This basically just replaces the for loop in the existing fixhosts file.

<quote>
for ip in $ips; do
  cat /etc/hosts | grep $ip >/dev/null
  if [ $? != 0 ]; then
    if (/usr/bin/host $ip 2>&1 > /dev/null)
    [ $? = 0 ]
    then
       TMP=`/usr/bin/host $ip | /usr/bin/tail -1 | /bin/awk '{print $5}'`
    else
        TMP=`hostname`
    fi
    /usr/bin/printf "$ip\t$TMP\n" >> /etc/hosts
  fi
  unset TMP
done
</quote>

The tail portion is to take into account DNS that has www.domain.tld and
domain.tld and mail.domain.tld all in DNS; could just as easily be "head -1"
to take the first entry returned...

PS:  not critizing, appreciate what you all do....
PPS:  if you prefer perl, I can redo fixhosts into a perl that does the same
thing...

-- 
Larry Smith
lesmith at ecsis.net



More information about the Blueonyx mailing list