Last active
August 29, 2015 14:25
-
-
Save angelbladex/9f95d5cd8177294384c7 to your computer and use it in GitHub Desktop.
Validate Ip Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Validate a IP address | |
function valid_ip() | |
{ | |
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 | |
} | |
if [[ ! -n "$1" ]]; then | |
echo "Missing data input" | |
exit | |
fi | |
IP=$1 | |
valid_ip $IP | |
if [[ $? -ne 0 ]];then | |
echo "Invalid Ip Address ($IP)" | |
else | |
echo "Valid Ip Address ($IP)" | |
fi | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment