Skip to content

Instantly share code, notes, and snippets.

@stoyanovgeorge
Last active March 11, 2019 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoyanovgeorge/332707e945b4a6660e865ff430460298 to your computer and use it in GitHub Desktop.
Save stoyanovgeorge/332707e945b4a6660e865ff430460298 to your computer and use it in GitHub Desktop.
#!/bin/bash
n='([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
m='([0-9]|[12][0-9]|3[012])'
IFS= read -rp 'Input: ' ipaddr
if [[ $ipaddr =~ ^$n(\.$n){3}/$m$ ]]; then
printf '"%s" is a valid CIDR\n' "$ipaddr"
elif [[ $ipaddr =~ ^$n(\.$n){3}$ ]]; then
printf '"%s" is a valid IP address\n' "$ipaddr"
else
printf '"%s" is not valid IP address\n' "$ipaddr"
fi
@stoyanovgeorge
Copy link
Author

checks if the address is in the form 22.22.22.22/32 or 22.22.22.22 and discards all invalid combinations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment