getlocalserverip(){
isFirstInput=$1
varri=0
while :
do
if [ $isFirstInput -eq 0 ];then
echotips "Please input local IP address[such as 192.168.0.11]:"
else
echotips "Please input local IP address again[such as 192.168.0.11]:"
fi
read tmpserverip #读入服务器IP#
if [ "$tmpserverip" = "" ]
then
echoerror "[Error]You must input IP address."
else
checklocalip "$tmpserverip"
case "$?" in
"0")
LOCALSERVERIP=$tmpserverip
break
;;
"1")
varri=`expr $varri + 1 `
if [ "$varri" -eq 5 ]
then
echoerror "[Error]You had input wrong serverip($tmpserverip) 5 times"
exit 1
fi
continue
;;
esac
fi
done
}
5、输入服务器IP地址
getserverip(){
INPUTIPNAME=$1
INPUTIPKEYNAME=$2
varri=0
while :
do
echotips "Please input ${INPUTIPNAME} server IP address[such as 192.168.0.11]:"
read tmpserverip #读入服务器IP#
if [ "$tmpserverip" = "" ]
then
echoerror "[Error]You must input IP address."
else
checkip "$tmpserverip"
case "$?" in
"0")
#填入的是一个有效字符串,输入的字符串进行赋值#
echo "$INPUTIPKEYNAME=$tmpserverip">>${TOP_PATH}/.hc.tmp
break
;;
"1")
echoerror "[Error]You input an illegal IP address"
varri=`expr $varri + 1 `
if [ "$varri" -eq 5 ]
then
echoerror "[Error]You had input wrong serverip($tmpserverip) 5 times"
exit 1
fi
continue
;;
esac
fi
done
}
6、校验IP地址格式
checkip()
inputorigin="$1"
strLen=$(expr length $inputorigin)
#判断IP地址的位数是否非法#
if [ "$strLen" -gt "15" ] || [ "$strLen" -lt "7" ]
then
echoerror "Wrong ip address,the length of ip address should be less than 15 and more than 7"
return 1
fi
#判断IP地址是否包含非法字符#
teststring=${inputorigin#*[!0-9.]}
if [ "$inputorigin" != "$teststring" ]
then
echoerror "IP address can only contain 0-9 and dot."
return 1
fi
for i in 0 1 2 3
do
if [ $i = 0 ] #判断第一个数字段是否非法#
then
ipval=${inputorigin%%.*}
if [ -z "$ipval" ]
then
echoerror "ip0val:$ipval "
return 1
fi
iptmp0=${inputorigin#*.}
if [ "$iptmp0" = "$inputorigin" ]
then
echoerror "$ipval error"
return 1
fi
if [ "$ipval" -le "0" ] #判断第一个数字段是否小于等于0#
then
echoerror "ip0 is smaller than 0 ,$ipval error"
return 1
elif [ "$ipval" -eq "127" ] #判断第一个数字段是否等于127#
then
echoerror "ip0 equals to 127 ,$ipval error"
return 1
elif [ "$ipval" -gt "223" ] #判断第一个数字段是否大于223#
then
echoerror "ip0 is greater than 223 ,$ipval error"
return 1
elif [ `echo $ipval | grep ^0` ]
then
echoerror "ip0 ,$ipval error"
return 1
fi
elif [ $i = 1 ] #判断第二个数字段是否非法#
then
ipval=${iptmp0%%.*}
if [ -z "$ipval" ]
then
echoerror "ip1val:$ipval is null"
return 1
fi
iptmp1=${iptmp0#*.}
if [ "$iptmp1" = "$iptmp0" ]
then
echoerror "$ipval error"
return 1
fi
if [ "$ipval" -lt "0" ] #判断第二个数字段是否小于0#
then
echoerror "ip1 is smaller than 0 ,$ipval error"
return 1
elif [ "$ipval" -gt "255" ] #判断第二个数字段是否大于255#
then
echoerror "ip1 is greater than 255 ,$ipval error"
return 1
elif [ `echo $ipval | grep ^0` ] && [ $(expr length $ipval) -gt 1 ]
then
echoerror "ip1 ,$ipval error"
return 1
fi
elif [ $i = 2 ] #判断第三个数字段是否非法#
then
ipval=${iptmp1%%.*}
if [ -z "$ipval" ]
then
echoerror "ip2val:$ipval is null"
return 1
fi
iptmp2=${iptmp1#*.}
if [ "$iptmp2" = "$iptmp1" ]
then
echoerror "$ipval error"
return 1
fi
if [ "$ipval" -lt "0" ] #判断第三个数字段是否小于0#
then
echoerror "ip2 is smaller than 0 ,$ipval error"
return 1
elif [ "$ipval" -gt "255" ] #判断第三个数字段是否大于255#
then
echoerror "ip2 is greater than 255 ,$ipval error"
return 1
elif [ `echo $ipval | grep ^0` ] && [ $(expr length $ipval) -gt 1 ]
then
echoerror "ip2 $ipval error"
return 1
fi
elif [ $i = 3 ] #判断第四个数字段是否非法#
then
ipval=${iptmp2%%.*}
if [ -z "$ipval" ]
then
echoerror "ip3val:$ipval is null"
return 1
fi
if [ `echo $iptmp2 | grep [.]` ] #判断第四个数字段后面是否还有.#
then
echoerror "wrongwrongwrong$inputorigin "
return 1
fi
if [ "$ipval" -lt "0" ] #判断第四个数字段是否小于0#
then
echoerror "ip3 is smaller than 0 ,$ipval error"
return 1
elif [ "$ipval" -gt "255" ] #判断第四个数字段是否大于255#
then
echoerror "ip3 is great than 255 ,$ipval error"
return 1
elif [ `echo $ipval | grep ^0` ] && [ $(expr length $ipval) -gt 1 ]
then
echoerror "ip3 ,$ipval error"
return 1
fi
fi
done
return 0
}