Implement a more efficient way to assign addresses: read /etc/hosts

only once into an array of shell variables, and then scan the array
to find entries matching the MAC address.
Associative arrays would really be handy here...
This commit is contained in:
Luigi Rizzo 2001-11-29 03:16:23 +00:00
parent 2209d8a27c
commit b8be92123e

View file

@ -48,43 +48,61 @@ get_ether() {
ether=""
set `ifconfig ${key}`
while [ "$1" != "" ] ; do
if [ "$1" = "ether" ] ; then
ether=$2
break
else
shift
fi
if [ "$1" = "ether" ] ; then
ether=$2
break
else
shift
fi
done
}
# read content from /etc/hosts into an array (needed later in fetch_hostname)
read_hosts() {
local i a b c key junk
i=""
while read a b c junk ; do
if [ "$a" = "#ethertable" ] ; then
i=0
elif [ "$i" != "" -a "X$a" = "X#" -a "$b" != "" ] ; then
eval eth_${i}=$b
eval eth_host_${i}=$c
i=$(($i+1))
fi
done < /etc/hosts
}
# set "hostname" using $1 (ethernet address) as search key in /etc/hosts
fetch_hostname() {
local a b c key junk
key=$1 # search key
hostname=""
while read a b c junk ; do
if [ "$a" = "#ethertable" ] ; then
hostname="."
elif [ "X$hostname" = "X." -a "X$a" = "X#" ] ; then
case X${key} in
X${b} ) # so we can use wildcards
hostname=$c
break
;;
esac
local i b key
key=$1
i=0
b="x"
if [ "${eth_0}" = "" ] ; then
read_hosts
fi
done < /etc/hosts
hostname=""
while [ "$b" != "" -a "${hostname}" = "" ] ; do
eval b=\${eth_${i}}
case X${key} in
X${b} ) # so we can use wildcards
eval hostname=\${eth_host_${i}}
break
;;
esac
i=$(($i+1))
done
echo "fetch_hostname for <${key}> returns <${hostname}>"
}
# sets "mask" using $1 (netmask name) as the search key in /etc/networks
fetch_mask() {
local a b c key
key=$1 # search key, typically hostname-netmask
local a b key junk
key=$1 # search key, typically hostname-netmask
mask=""
while read a b c; do # key mask otherstuff
while read a b junk; do # key mask otherstuff
case X${key} in
X${a} ) # we can use wildcards
X${a} ) # we can use wildcards
mask=$b
break
;;