diff options
author | Aaron Ball <nullspoon@oper.io> | 2018-11-02 10:50:23 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2018-11-02 10:50:23 -0600 |
commit | 1c9ef1f8a936d71c19ff07149e97e443923fc7fd (patch) | |
tree | 8c3e1884a46afba24f9a12f9b181d4563c594a17 | |
parent | cbdd87023c63734e996b058436c4cf82b81c5b5e (diff) | |
download | dnsbl-check-1c9ef1f8a936d71c19ff07149e97e443923fc7fd.tar.gz dnsbl-check-1c9ef1f8a936d71c19ff07149e97e443923fc7fd.tar.xz |
Added IPv4 validity check
This adds the is_ipv4 function that checks the input string for ipv4
validity. Main function will now exit early if provided IP is invalid.
-rwxr-xr-x | dnsbl-check.sh | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/dnsbl-check.sh b/dnsbl-check.sh index f3632bd..9e202b6 100755 --- a/dnsbl-check.sh +++ b/dnsbl-check.sh @@ -25,6 +25,23 @@ export CRESET=$'\e[0m' export LIST=${LIST:-dnsbls.txt} +# is_ipv4: +# Checks if the provided string is a valid IPv4 address. +# +# @str String to check +# @return (stdout) 1 == valid ip, 0 == invalid ip +is_ipv4() { + local str="${1}" + local match + match=$(printf "${str}" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$') + if [ "${match}" = "${str}" ]; then + printf 1 + return 0; + fi + printf 0; + return 1; +} + # main: # Ye olde' main. @@ -49,6 +66,11 @@ main() { dnsbls=($(grep -v '^#' "${LIST}")) fi + if [ $(is_ipv4 "${ip}") = 0 ]; then + printf "Provided IP '%s' is not a valid IPv4 address\n" "${ip}" + return 1 + fi + # If the terminal is not a char terminal (eg: someone is using less, more, # cat, etc), we don't want to print escape codes because they will get # mangled by the tool most likely. |