#!/bin/bash
# Test an IP address for validity:
# Usage:
# is_ip IP_ADDRESS
# if is_ip IP_ADDRESS; then return 1; else return 0; fi
#
is_ip(){
input=$1
octet1=$(echo $input | cut -d "." -f1)
octet2=$(echo $input | cut -d "." -f2)
octet3=$(echo $input | cut -d "." -f3)
octet4=$(echo $input | cut -d "." -f4)
stat=0
if [[ $input =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && [ $octet1 -le 255 ] && [ $octet2 -le 255 ] && [ $octet3 -le 255 ] && [ $octet4 -le 255 ];
then
stat=1
fi
echo "IP $input is $stat."
return $stat
}
if [ -z $1 ];
then
echo "Usage: sh is_ip.sh IP_ADDRESS"
exit 0
else
is_ip $1
fi
Ref: http://www.linuxjournal.com/content/validating-ip-address-bash-script
No comments:
Post a Comment