IP检测IP是否有效

IP检测IP是否有效

#!/bin/bash
c_version(){
echo "# Author: royoy
# Mail  : royoy@royoy.com
# Ver   : v0.02
# Date  : June/2015
# Desc  : check if IP is valid
# Usage : ipcheck [-p] ip1 ip2 ...
#   eg  : ipcheck -p 1.1.10.2
#   eg  : ipcheck 1.1.10.2"
}

p_notice=0

check_ip_valid(){
        local flag=0
	if [[ "-h -v --help -H -version --version -V" =~ "$1" ]];then c_version;exit;fi
        for IP in $*
        do
                while  :
                do
                        echo "$IP" | egrep -q '^([0-9]{1,3}(\.\b|$)){4}$'
                        [ $? -eq 0 ] && {
                                valid=1
                                for number in ${IP//./ }
                                do
                                        [ $number -gt 255 ] && valid=0 && break
                                done
                                [ $valid -eq 1 ] && break
                        }
                        echo "Incorrect IP format [ $IP ]"
                        flag=$((flag+1))
                        break
                done
        done
        [ $flag -gt 0 ] && {
                [ $flag -eq 1 ] && {
                        tmp1="is"
                        tmp2="IP"
                }
                [ $p_notice -eq 1 ] && echo "There ${tmp1:-are} $flag invalid ${tmp2:-IPs}."
        }
        return $flag
}

if [[ $1 =~ "-p" ]];then p_notice=1;shift;fi

check_ip_valid "$@"
# End