Skip to content

Instantly share code, notes, and snippets.

@SkypLabs
Last active January 6, 2017 01:12
Show Gist options
  • Save SkypLabs/8758fa2aa63201b028d0ac680b2a90c4 to your computer and use it in GitHub Desktop.
Save SkypLabs/8758fa2aa63201b028d0ac680b2a90c4 to your computer and use it in GitHub Desktop.
Bash function for testing an IPv4 address
is_ipv4() {
local -r regex='^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
[[ $1 =~ $regex ]]
return $?
}
is_ipv4 $ip
if [ "$?" -ne 0 ]
then
echo "[x] $ip is not an IPv4"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment