Operating System - HP-UX
1826451 Members
4316 Online
109692 Solutions
New Discussion

Re: /etc/hosts script required: quick one!

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

/etc/hosts script required: quick one!

Hi all, back into my lab script creation time..

I'm looking for a script (don't ask why ;)) that:

1> checks for an undefined lan interface (can assume lan1 is free, but best to check)
ie: ifconfig lan1
if ERR
then
ifconfig lan1 192.168.1.1
else
ifconfig lan2
if ERR
then
ifconfig lan2 192.168.1.1
etc..
end

2> after assigning this lan,
need to look at lan0 (that hostname is tied to - can assume 0..)
lets say it's 10.1.2.3
then grep the hostname /lan0 ip in /etc/hosts.

3> /etc/hosts will then need to be modified to end up
with
192.168.1.1 hostname hostalias
127.0.0.1 loopback localhost
10.1.2.3 hostname hostalias

rather than the previous
127.0.0.1 loopback localhost
10.1.2.3 hostname hostalias

+ all the rest of the stuff in /etc/hosts remaining.

Thanks!
Bill


It works for me (tm)
3 REPLIES 3
Paula J Frazer-Campbell
Honored Contributor
Solution

Re: /etc/hosts script required: quick one!

Bill

I use the following to give the status of a lan, it can be modified to notify of an un-configured interface, one found then run through the config of this found interface, either pulling the info from the script, command line or config file.


Etc/hosts will have to have an annotation in it to distinguish Lan0 so that its info can be pulled.

Just a few ideas.

---------------------------------------------
#! /usr/bin/sh
/usr/bin/uname -a
/usr/sbin/lanscan
ppas="0 1"
for ppa in $ppas
do
stat=`/usr/sbin/lanadmin 2>/dev/null <l
ppa $ppa
d
q

EOF
`
echo $ppa --- $stat
done
----------------------------------------------
Paula
If you can spell SysAdmin then you is one - anon
Bill McNAMARA_1
Honored Contributor

Re: /etc/hosts script required: quick one!

managed to throw something together..
But would prefer a more elegant ip comparisson before changing!

Still up for points!

#!/usr/bin/ksh
#
# add the hostname to the /etc/hosts first

THEIP=192.168.1.99
THEIP2=10.1.2.3

for lan in $(lanscan | grep lan | awk {'print $5'})
do
ifconfig $lan 1>/dev/null 2>/dev/null
if [[ $? = 0 ]]; then
echo "> $lan used \c: "
#
# making sure IP not used already
#
ifconfig $lan | grep inet | awk {'print $2'} | grep $THEIP
if [[ $? = 0 ]]; then
echo "> $THEIP already in use here"
echo "> IP to set now $THEIP2"
THEIP=$THEIP2
fi
else
echo "> using $lan"
ifconfig $lan $THEIP
break
fi
done

set -x

grep \# /etc/hosts > /tmp/hosts
echo "$THEIP\t$(hostname)\t$(hostname)." >> /tmp/hosts
grep -v \# /etc/hosts >> /tmp/hosts
It works for me (tm)
Volker Borowski
Honored Contributor

Re: /etc/hosts script required: quick one!

Hi Bill,

I do not know if you can rule out my thoughts because of your environment, but:

1) a dot in a grep expression stands for a single character wildcard ! See this :

# echo 1.1.1.1 > greptarget
# echo 121.121.2.2 >> greptarget

# grep 1.1.1.1 greptarget
1.1.1.1
121.121.2.2

# grep 1\.1\.1\.1 greptarget
1.1.1.1
121.121.2.2

# grep -e '1\.1\.1\.1' greptarget
1.1.1.1

So your THEIP variable will probably resolve to more than one possible target in /etc/hosts (probably not) ?
To be correct a logical expression ( -e ) with backslashed dots will be safe.

2) I have a system here with an unconfigured token-ring card, that does not show up in lanscan, although it is visible in ioscan. ifconfig lan1 says the interface does not exist, although it is physically in the box,
and had been configuered years ago. This might be token-ring specific, but consider that diffrent physical lanX can behave diffrent from ethernet (FDDI ? other ...).

3) If a box is configuered without LAN upon installation, and the network is configured later. The initial binding of the hostname is against the loopback interface (lo0:) which also does not appear in lanscan. Do not know if this makes any diffrence in your case.

4) Your modification of /tmp/hosts will sort all comments to the top if I analyzed correctly. For me this would not be good, because I have several comment headers for diffrent networks like this
# net one
a.b.c.1
a.b.c.2
# net two
c.d.e.4
c.d.e.5

Have a nice weekend
volker