utilities.san revision 7c478bd95313f5f23a4c958a745db2134aa03244
#!/usr/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#ident "%Z%%M% %I% %E% SMI"
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#
# Process arguments.
#
process_args()
{
DEBUG=
DHCPRSRC=
DHCPPATH=
DHCPRSRC_NEW=
DHCPPATH_NEW=
ERR=
USAGE="Usage: %s: [-d]"
while getopts ${VALIDOPTS} name
do
case $name in
d) DEBUG="-d";;
r) DHCPRSRC="${OPTARG}";;
p) DHCPPATH="${OPTARG}";;
R) DHCPRSRC_NEW="${OPTARG}";;
P) DHCPPATH_NEW="${OPTARG}";;
?) ERR=1;;
esac
done
#
# If the resource is a valid option, then it is a required one
#
echo ${VALIDOPTS} | grep r: >/dev/null
if [ $? == 0 ]
then
USAGE="${USAGE} -r resource"
if [ -z "${DHCPRSRC}" ]
then
ERR=1
fi
fi
#
# If the path is a valid option, then it is a required one
#
echo ${VALIDOPTS} | grep p: >/dev/null
if [ $? == 0 ]
then
USAGE="${USAGE} -p path"
if [ -z "${DHCPPATH}" ]
then
ERR=1
fi
fi
#
# If the conversion resource is a valid option, then it is a required one
#
echo ${VALIDOPTS} | grep R: >/dev/null
if [ $? == 0 ]
then
USAGE="${USAGE} -R resource"
if [ -z "${DHCPRSRC_NEW}" ]
then
ERR=1
fi
fi
#
# If the conversion path is a valid option, then it is a required one
#
echo ${VALIDOPTS} | grep P: >/dev/null
if [ $? == 0 ]
then
USAGE="${USAGE} -P path"
if [ -z "${DHCPPATH_NEW}" ]
then
ERR=1
fi
fi
if [ ! -z "${ERR}" ]
then
printf "$USAGE\n" $0
exit -1
fi
}
#
# Return the primary interface's IP address.
#
get_server_ip()
{
awk '$1 ~ /^[0-9]/ && $2 == "'${SRVNAME}'" { printf "%s", $1; exit }' /etc/inet/hosts
}
#
# Based on the network specification, determine whether or not network is
# subnetted or supernetted.
# Given a dotted IP network number, convert it to the default class
# network.(used to detect subnetting). Requires one argument, the
# network number. (e.g. 10.0.0.0) Echos the default network and default
# mask for success, null if error.
#
get_default_class()
{
NN01=${1%%.*}
tmp=${1#*.}
NN02=${tmp%%.*}
tmp=${tmp#*.}
NN03=${tmp%%.*}
tmp=${tmp#*.}
NN04=${tmp%%.*}
RETNET=""
RETMASK=""
typeset -i16 ONE=10#${1%%.*}
typeset -i10 X=$((${ONE}&16#f0))
if [ ${X} -eq 224 ]
then
# Multicast
typeset -i10 TMP=$((${ONE}&16#f0))
RETNET="${TMP}.0.0.0"
RETMASK="240.0.0.0"
fi
typeset -i10 X=$((${ONE}&16#80))
if [ -z "${RETNET}" -a ${X} -eq 0 ]
then
# Class A
RETNET="${NN01}.0.0.0"
RETMASK="255.0.0.0"
fi
typeset -i10 X=$((${ONE}&16#c0))
if [ -z "${RETNET}" -a ${X} -eq 128 ]
then
# Class B
RETNET="${NN01}.${NN02}.0.0"
RETMASK="255.255.0.0"
fi
typeset -i10 X=$((${ONE}&16#e0))
if [ -z "${RETNET}" -a ${X} -eq 192 ]
then
# Class C
RETNET="${NN01}.${NN02}.${NN03}.0"
RETMASK="255.255.255.0"
fi
print - ${RETNET} ${RETMASK}
unset NNO1 NNO2 NNO3 NNO4 RETNET RETMASK X ONE
}
#
# Based on the nsswitch setting, query the netmasks table for a netmask.
# Accepts one argument, a dotted IP address.
#
get_netmask()
{
MTMP=`getent netmasks ${1} | awk '{ print $2 }'`
if [ ! -z "${MTMP}" ]
then
print - ${MTMP}
fi
}
# Given a network number and subnetmask, return the broadcast address.
get_bcast_addr()
{
typeset -i16 NNO1=10#${1%%.*}
tmp=${1#*.}
typeset -i16 NNO2=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NNO3=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NNO4=10#${tmp%%.*}
typeset -i16 NMO1=10#${2%%.*}
tmp=${2#*.}
typeset -i16 NMO2=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NMO3=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NMO4=10#${tmp%%.*}
typeset -i16 ONE
typeset -i16 TWO
typeset -i16 THREE
typeset -i16 FOUR
let ONE=\~${NMO1}\|${NNO1}
let ONE=${ONE}\&16#ff
let TWO=\~${NMO2}\|${NNO2}
let TWO=${TWO}\&16#ff
let THREE=\~${NMO3}\|${NNO3}
let THREE=${THREE}\&16#ff
let FOUR=\~${NMO4}\|${NNO4}
let FOUR=${FOUR}\&16#ff
typeset -i10 ONE
typeset -i10 TWO
typeset -i10 THREE
typeset -i10 FOUR
print - "${ONE}.${TWO}.${THREE}.${FOUR}"
}
# Given a network number and subnetmask, return the subnet address.
get_subnet_addr()
{
typeset -i16 NNO1=10#${1%%.*}
tmp=${1#*.}
typeset -i16 NNO2=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NNO3=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NNO4=10#${tmp%%.*}
typeset -i16 NMO1=10#${2%%.*}
tmp=${2#*.}
typeset -i16 NMO2=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NMO3=10#${tmp%%.*}
tmp=${tmp#*.}
typeset -i16 NMO4=10#${tmp%%.*}
typeset -i16 ONE
typeset -i16 TWO
typeset -i16 THREE
typeset -i16 FOUR
let ONE=${NMO1}\&${NNO1}
let TWO=${NMO2}\&${NNO2}
let THREE=${NMO3}\&${NNO3}
let FOUR=${NMO4}\&${NNO4}
typeset -i10 ONE
typeset -i10 TWO
typeset -i10 THREE
typeset -i10 FOUR
print - "${ONE}.${TWO}.${THREE}.${FOUR}"
}
#
# Given a macro definition defined in MACRO_DEFINITION and a symbol/value string
# defined in SRCH, search the macro definition for the string. If not found, abort.
# If found, remove the string from the definition.
#
macro_find_and_replace()
{
VAL=`expr "${MACRO_DEFINITION}" : .*"${SRCH}".*`
if [ "${VAL}" = "0" ]
then
echo "${SRCH} not defined as part of the macro definition"
echo "${TESTNAME} - Test failed!"
exit 1
fi
SRCH=`echo "${SRCH}" | cut -c2-`
MACRO_DEFINITION=`echo "${MACRO_DEFINITION}" | sed s/"${SRCH}"//`
}
#
# Given a macro or option definition, returns its value.
#
function get_value
{
echo $* | cut -d ' ' -f 3-
}