summaryrefslogtreecommitdiff
path: root/dnsbl-check.sh
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2018-11-02 10:50:23 -0600
committerAaron Ball <nullspoon@oper.io>2018-11-02 10:50:23 -0600
commit1c9ef1f8a936d71c19ff07149e97e443923fc7fd (patch)
tree8c3e1884a46afba24f9a12f9b181d4563c594a17 /dnsbl-check.sh
parentcbdd87023c63734e996b058436c4cf82b81c5b5e (diff)
downloaddnsbl-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.
Diffstat (limited to 'dnsbl-check.sh')
-rwxr-xr-xdnsbl-check.sh22
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.

Generated by cgit