From cfed3d59e9d86d6c623424ad2d4fe6af70fbd011 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Thu, 16 Feb 2017 16:49:14 -0800 Subject: [PATCH 1/4] Begin fixing IP detection functions. --- advanced/Scripts/piholeDebug.sh | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/advanced/Scripts/piholeDebug.sh b/advanced/Scripts/piholeDebug.sh index 74d2f05c..d374cfad 100755 --- a/advanced/Scripts/piholeDebug.sh +++ b/advanced/Scripts/piholeDebug.sh @@ -26,7 +26,7 @@ ADLISTFILE="/etc/pihole/adlists.list" PIHOLELOG="/var/log/pihole.log" WHITELISTMATCHES="/tmp/whitelistmatches.list" -IPV6_READY=false +IPV6_READY TIMEOUT=60 # Header info and introduction cat << EOM @@ -180,7 +180,7 @@ processor_check() { ipv6_check() { # Check if system is IPv6 enabled, for use in other functions - if [[ $IPv6_address ]]; then + if [[ $IPV6_ADDRESS ]]; then ls /proc/net/if_inet6 &>/dev/null && IPV6_READY=true return 0 else @@ -196,21 +196,30 @@ ip_check() { # Check if IPv6 enabled local IPv6_interface local IPv4_interface - ipv6_check && IPv6_interface=${piholeInterface:-$(ip -6 r | grep default | cut -d ' ' -f 5)} + if ipv6_check; then + IPv6_interface=${PIHOLE_INTERFACE:-$(ip -6 r | grep default | cut -d ' ' -f 5)} + echo ${PIHOLE_INTERFACE} + echo ${IPv6_interface} + fi # If declared in setupVars.conf use it, otherwise defer to default # http://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash - IPv4_interface=${piholeInterface:-$(ip r | grep default | cut -d ' ' -f 5)} + IPv4_interface=${PIHOLE_INTERFACE:-$(ip r | grep default | cut -d ' ' -f 5)} - if [[ IPV6_READY ]]; then - local IPv6_addr_list="$(ip a | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "inet6") print $(i+1) }')" \ - && (log_write "${IPv6_addr_list}" && echo "::: IPv6 addresses located") \ - || log_echo "No IPv6 addresses found." + if [[ "$IPV6_READY" = true ]]; then + local IPv6_addr_list="$(ip a | awk -F ' ' '{ for(i=1;i<=NF;i++) if ($i == "inet6") print $(i+1) }')" + if [[ -n ${IPv6_addr_list} ]]; then + log_write "${IPv6_addr_list}" + echo "::: IPv6 addresses located" + else + log_echo "No IPv6 addresses found." + return 0 + fi local IPv6_def_gateway=$(ip -6 r | grep default | cut -d ' ' -f 3) if [[ $? = 0 ]] && [[ -n ${IPv6_def_gateway} ]]; then echo -n "::: Pinging default IPv6 gateway: " - local IPv6_def_gateway_check="$(ping6 -q -W 3 -c 3 -n "${IPv6_def_gateway}" -I "${IPv6_interface}"| tail -n3)" \ + local IPv6_def_gateway_check="$(ping6 -q -W 3 -c 3 -n ${IPv6_def_gateway} -I ${IPv6_interface} | tail -n3)" \ && echo "Gateway Responded." \ || echo "Gateway did not respond." block_parse "${IPv6_def_gateway_check}" @@ -223,27 +232,26 @@ ip_check() { else log_echo="No IPv6 Gateway Detected" fi + fi -local IPv4_addr_list="$(ip a | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "inet") print $(i+1) }')" \ + local IPv4_addr_list="$(ip a | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "inet") print $(i+1) }')" \ && (block_parse "${IPv4_addr_list}" && echo "::: IPv4 addresses located")\ || log_echo "No IPv4 addresses found." local IPv4_def_gateway=$(ip r | grep default | cut -d ' ' -f 3) if [[ $? = 0 ]]; then echo -n "::: Pinging default IPv4 gateway: " - local IPv4_def_gateway_check="$(ping -q -w 3 -c 3 -n "${IPv4_def_gateway}" -I "${IPv4_interface}" | tail -n3)" \ + local IPv4_def_gateway_check="$(ping -q -w 3 -c 3 -n ${IPv4_def_gateway} -I ${IPv4_interface} | tail -n3)" \ && echo "Gateway responded." \ || echo "Gateway did not respond." block_parse "${IPv4_def_gateway_check}" echo -n "::: Pinging Internet via IPv4: " - local IPv4_inet_check="$(ping -q -w 5 -c 3 -n 8.8.8.8 -I "${IPv4_interface}" | tail -n3)" \ + local IPv4_inet_check="$(ping -q -w 5 -c 3 -n 8.8.8.8 -I ${IPv4_interface} | tail -n3)" \ && echo "Query responded." \ || echo "Query did not respond." block_parse "${IPv4_inet_check}" fi - - fi } port_check() { From 472708376df99cdf707c92611c92601cf98a5c56 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Thu, 16 Feb 2017 18:30:15 -0800 Subject: [PATCH 2/4] Split out IP checks. --- advanced/Scripts/piholeDebug.sh | 97 ++++++++++++++------------------- 1 file changed, 40 insertions(+), 57 deletions(-) diff --git a/advanced/Scripts/piholeDebug.sh b/advanced/Scripts/piholeDebug.sh index d374cfad..97f9112a 100755 --- a/advanced/Scripts/piholeDebug.sh +++ b/advanced/Scripts/piholeDebug.sh @@ -26,7 +26,6 @@ ADLISTFILE="/etc/pihole/adlists.list" PIHOLELOG="/var/log/pihole.log" WHITELISTMATCHES="/tmp/whitelistmatches.list" -IPV6_READY TIMEOUT=60 # Header info and introduction cat << EOM @@ -181,77 +180,61 @@ processor_check() { ipv6_check() { # Check if system is IPv6 enabled, for use in other functions if [[ $IPV6_ADDRESS ]]; then - ls /proc/net/if_inet6 &>/dev/null && IPV6_READY=true + ls /proc/net/if_inet6 &>/dev/null return 0 else return 1 fi } - ip_check() { - header_write "IP Address Information" - # Get the current interface for Internet traffic + local protocol=${1} - # Check if IPv6 enabled - local IPv6_interface - local IPv4_interface - if ipv6_check; then - IPv6_interface=${PIHOLE_INTERFACE:-$(ip -6 r | grep default | cut -d ' ' -f 5)} - echo ${PIHOLE_INTERFACE} - echo ${IPv6_interface} - fi - # If declared in setupVars.conf use it, otherwise defer to default - # http://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash - IPv4_interface=${PIHOLE_INTERFACE:-$(ip r | grep default | cut -d ' ' -f 5)} + local ip_addr_list="$(ip -${protocol} addr show dev ${PIHOLE_INTERFACE} | awk -F ' ' '{ for(i=1;i<=NF;i++) if ($i ~ '/^inet/') print $(i+1) }')" + if [[ -n ${ip_addr_list} ]]; then + log_write "IPv${protocol} on ${PIHOLE_INTERFACE}" + log_write "${ip_addr_list}" + echo "::: IPv${protocol} addresses located on ${PIHOLE_INTERFACE}" + ip_ping_check ${protocol} + return $(( 0 + $? )) + else + log_echo "No IPv${protocol} found on ${PIHOLE_INTERFACE}" + return 1 + fi +} +ip_ping_check() { - if [[ "$IPV6_READY" = true ]]; then - local IPv6_addr_list="$(ip a | awk -F ' ' '{ for(i=1;i<=NF;i++) if ($i == "inet6") print $(i+1) }')" - if [[ -n ${IPv6_addr_list} ]]; then - log_write "${IPv6_addr_list}" - echo "::: IPv6 addresses located" - else - log_echo "No IPv6 addresses found." - return 0 - fi + local protocol=${1} + local cmd - local IPv6_def_gateway=$(ip -6 r | grep default | cut -d ' ' -f 3) - if [[ $? = 0 ]] && [[ -n ${IPv6_def_gateway} ]]; then - echo -n "::: Pinging default IPv6 gateway: " - local IPv6_def_gateway_check="$(ping6 -q -W 3 -c 3 -n ${IPv6_def_gateway} -I ${IPv6_interface} | tail -n3)" \ - && echo "Gateway Responded." \ - || echo "Gateway did not respond." - block_parse "${IPv6_def_gateway_check}" - - echo -n "::: Pinging Internet via IPv6: " - local IPv6_inet_check=$(ping6 -q -W 3 -c 3 -n 2001:4860:4860::8888 -I "${IPv6_interface}"| tail -n3) \ - && echo "Query responded." \ - || echo "Query did not respond." - block_parse "${IPv6_inet_check}" - else - log_echo="No IPv6 Gateway Detected" - fi + if [[ ${protocol} == "6" ]]; then + cmd="ping6" + else + cmd="ping" fi - local IPv4_addr_list="$(ip a | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "inet") print $(i+1) }')" \ - && (block_parse "${IPv4_addr_list}" && echo "::: IPv4 addresses located")\ - || log_echo "No IPv4 addresses found." + local ip_def_gateway=$(ip -${protocol} route | grep default | cut -d ' ' -f 3) + if [[ -n ${ip_def_gateway} ]]; then + echo -n "::: Pinging default IPv${protocol} gateway: " + if ! ping_gateway="$(${cmd} -q -W 3 -c 3 -n ${ip_def_gateway} -I ${PIHOLE_INTERFACE} | tail -n 3)"; then + echo "Gateway did not respond." + return 1 + else + echo "Gateway responded." + log_write "${ping_gateway}" + return 0 + fi + fi +} - local IPv4_def_gateway=$(ip r | grep default | cut -d ' ' -f 3) - if [[ $? = 0 ]]; then - echo -n "::: Pinging default IPv4 gateway: " - local IPv4_def_gateway_check="$(ping -q -w 3 -c 3 -n ${IPv4_def_gateway} -I ${IPv4_interface} | tail -n3)" \ - && echo "Gateway responded." \ - || echo "Gateway did not respond." - block_parse "${IPv4_def_gateway_check}" +ip_check_wrapper() { + header_write "IP Address Information" - echo -n "::: Pinging Internet via IPv4: " - local IPv4_inet_check="$(ping -q -w 5 -c 3 -n 8.8.8.8 -I ${IPv4_interface} | tail -n3)" \ - && echo "Query responded." \ - || echo "Query did not respond." - block_parse "${IPv4_inet_check}" + if ipv6_check; then + ip_check "6" fi + ip_check "4" } port_check() { @@ -364,7 +347,7 @@ distro_check || echo "Distro Check soft fail" # Gather processor type processor_check || echo "Processor Check soft fail" -ip_check +ip_check_wrapper daemon_check lighttpd http daemon_check dnsmasq domain From 2aa3a109a0f8eb9693754ab9a01ac859ad47f83d Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Thu, 16 Feb 2017 18:38:05 -0800 Subject: [PATCH 3/4] Ping gateways and Internet. --- advanced/Scripts/piholeDebug.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/advanced/Scripts/piholeDebug.sh b/advanced/Scripts/piholeDebug.sh index 97f9112a..2733aeaa 100755 --- a/advanced/Scripts/piholeDebug.sh +++ b/advanced/Scripts/piholeDebug.sh @@ -210,8 +210,10 @@ ip_ping_check() { if [[ ${protocol} == "6" ]]; then cmd="ping6" + g_addr="2001:4860:4860::8888" else cmd="ping" + g_addr="8.8.8.8" fi local ip_def_gateway=$(ip -${protocol} route | grep default | cut -d ' ' -f 3) @@ -223,9 +225,17 @@ ip_ping_check() { else echo "Gateway responded." log_write "${ping_gateway}" - return 0 + fi + echo -n "::: Pinging Internet via IPv${protocol}: " + if ! ping_inet="$(${cmd} -q -W 3 -c 3 -n ${g_addr} -I ${PIHOLE_INTERFACE} | tail -n 3)"; then + echo "Query did not respond." + return 1 + else + echo "Query responded." + log_write "${ping_inet}" fi fi + return 0 } ip_check_wrapper() { From 053b38e0bd16cdb57a69fbce15a2fffeaf14b588 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Thu, 16 Feb 2017 19:45:44 -0800 Subject: [PATCH 4/4] Set up for IP matching in detection. --- advanced/Scripts/piholeDebug.sh | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/advanced/Scripts/piholeDebug.sh b/advanced/Scripts/piholeDebug.sh index 2733aeaa..1415d975 100755 --- a/advanced/Scripts/piholeDebug.sh +++ b/advanced/Scripts/piholeDebug.sh @@ -189,10 +189,13 @@ ipv6_check() { ip_check() { local protocol=${1} + local gravity=${2} local ip_addr_list="$(ip -${protocol} addr show dev ${PIHOLE_INTERFACE} | awk -F ' ' '{ for(i=1;i<=NF;i++) if ($i ~ '/^inet/') print $(i+1) }')" if [[ -n ${ip_addr_list} ]]; then log_write "IPv${protocol} on ${PIHOLE_INTERFACE}" + log_write "Gravity configured for: ${2:-NOT CONFIGURED}" + log_write "----" log_write "${ip_addr_list}" echo "::: IPv${protocol} addresses located on ${PIHOLE_INTERFACE}" ip_ping_check ${protocol} @@ -204,7 +207,6 @@ ip_check() { } ip_ping_check() { - local protocol=${1} local cmd @@ -234,19 +236,12 @@ ip_ping_check() { echo "Query responded." log_write "${ping_inet}" fi + else + log_echo " No gateway detected." fi return 0 } -ip_check_wrapper() { - header_write "IP Address Information" - - if ipv6_check; then - ip_check "6" - fi - ip_check "4" -} - port_check() { local lsof_value @@ -357,7 +352,8 @@ distro_check || echo "Distro Check soft fail" # Gather processor type processor_check || echo "Processor Check soft fail" -ip_check_wrapper +ip_check 6 ${IPV6_ADDRESS} +ip_check 4 ${IPV4_ADDRESS} daemon_check lighttpd http daemon_check dnsmasq domain