Created
January 25, 2017 22:18
-
-
Save beatwiz/4def99edbc43c31919af4a3918472350 to your computer and use it in GitHub Desktop.
[shell] 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 | |
# Test an IP address for validity: | |
# Usage: | |
# valid_ip IP_ADDRESS | |
# if [[ $? -eq 0 ]]; then echo good; else echo bad; fi | |
# OR | |
# if valid_ip IP_ADDRESS; then echo good; else echo bad; fi | |
# | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment