Last active
March 11, 2019 09:37
-
-
Save stoyanovgeorge/332707e945b4a6660e865ff430460298 to your computer and use it in GitHub Desktop.
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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
checks if the address is in the form 22.22.22.22/32 or 22.22.22.22 and discards all invalid combinations.