Bash script to check for valid IP

Script to check for valid ip

#!/bin/bash
function validateIP()
        {
         local ip=$1
         local stat=1
         if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
                OIFS=$IFS
                IFS='.'
                ip=($ip)
                IFS=$OIFS
                [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
                && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
                stat=$?
        fi
        
        return $stat
}
ipToCheck="192.168.1.1"
validateIP $ipToCheck

if [[ $? -ne 0 ]];then
	echo "Invalid IP Address"
fi

You may also like...