3 # lc_servip - script for verifying the service IP and the real
4 # interface IP in a remote host are in the same subnet
6 ###############################################################################
12 Usage: `basename $0` <service IPaddr> <hostname>
14 service IPaddr the IP address to failover
15 hostname the hostname of the remote node
27 REMOTE=${REMOTE:-"ssh -x -q"}
29 # Check whether the reomte command is pdsh
31 if [ "${REMOTE}" = "${REMOTE#*pdsh}" ]; then
39 # inSameIPsubnet serviceIPaddr interfaceIPaddr mask
41 # Given two IP addresses and a subnet mask determine if these IP
42 # addresses are in the same subnet. If they are, return 0, else return 1.
47 declare -ia ip1 ip2 # IP addresses given
48 declare -i quad1 quad2 # calculated quad words
51 # Remove '.' characters from dotted decimal notation and save
54 # 192.168.1.163 -> array[0] = 192
60 for quad in $(echo $1 | awk -F. '{print $1 " " $2 " " $3 " " $4}')
67 for quad in $(echo $2 | awk -F. '{print $1 " " $2 " " $3 " " $4}')
74 for quad in $(echo $3 | awk -F. '{print $1 " " $2 " " $3 " " $4}')
81 # For each quad word, logically AND the IP address with the subnet
82 # mask to get the network/subnet quad word. If the resulting
83 # quad words for both IP addresses are the same they are in the
88 let $((quad1=${ip1[n]} & ${mask[n]}))
89 let $((quad2=${ip2[n]} & ${mask[n]}))
91 if [ $quad1 != $quad2 ]; then
92 echo >&2 $"`basename $0`: Service IP address $1 and"\
93 "real interface IP address $2 are in"\
95 return 1 # in different subnets
99 return 0 # in the same subnet, all quad words matched
103 # findInterface IPaddr hostname
105 # Given a target IP address and a hostname, find the interface in which
106 # this address is configured. If found return 0, if not return 1. The
107 # interface name is returned to stdout.
129 if [ "$line" = "" ]; then # go to next interface
135 while [ $# -gt 0 ]; do
139 if [ -n "$addr" -a "$addr" = "$target" ]
149 done < <(${REMOTE} $hostname /sbin/ifconfig)
151 echo >&2 "`basename $0`: Cannot find the interface in which" \
152 "$target is configured in the host $hostname!"
157 # findNetmask interface hostname
159 # Given an interface find the netmask addresses associated with it.
160 # Return 0 when found, else return 1. The netmask is returned to stdout.
177 while [ $# -gt 0 ]; do
180 echo ${1##*:} # return netmask addr
186 done < <(${REMOTE} $hostname /sbin/ifconfig $target)
188 echo >&2 "`basename $0`: Cannot find the netmask associated with" \
189 "the interface $target in the host $hostname!"
194 # check_srvIPaddr serviceIPaddr hostname
196 # Given a service IP address and hostname, check whether the service IP address
197 # and the real interface IP address of hostname are in the same subnet.
198 # If they are, return 0, else return 1.
204 declare srv_IPaddr=$1
207 # Get the corresponding IP address of the hostname from /etc/hosts table
208 real_IPaddr=`egrep "[[:space:]]$hostname([[:space:]]|$)" /etc/hosts \
210 if [ -z "$real_IPaddr" ]; then
211 echo >&2 "`basename $0`: Hostname $hostname does not exist in" \
212 "the local /etc/hosts table!"
216 if [ ${#real_IPaddr} -gt 15 ]; then
217 echo >&2 "`basename $0`: More than one IP address line" \
218 "corresponding to $hostname in the local" \
223 # Get the interface in which the real IP address is configured
224 real_intf=$(findInterface $real_IPaddr $hostname)
225 if [ $? -ne 0 ]; then
228 real_intf=${real_intf%%:*}
230 # Get the netmask address associated with the real interface
231 netmask=$(findNetmask $real_intf $hostname)
232 if [ $? -ne 0 ]; then
236 # Determine if the service IP address and the real IP address
237 # are in the same subnet
238 inSameIPsubnet $srv_IPaddr $real_IPaddr $netmask
239 if [ $? -ne 0 ]; then
246 # Check service IP address
247 if ! check_srvIPaddr $1 $2; then