From 1c9ef1f8a936d71c19ff07149e97e443923fc7fd Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Fri, 2 Nov 2018 10:50:23 -0600 Subject: 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. --- dnsbl-check.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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. -- cgit v1.2.3