From 07ac2f853316618360151d0a2bd0dd68b69d6e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Fri, 25 Mar 2022 17:21:08 +0100 Subject: [PATCH 01/14] Use FTL's API endpoint to get Pi-hole's status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 82 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/padd.sh b/padd.sh index d7081a60..3dccad56 100755 --- a/padd.sh +++ b/padd.sh @@ -149,6 +149,8 @@ GetSummaryInformation() { clients=$(grep "unique_clients" <<< "${summary}" | grep -Eo "[0-9]+$") + blocking_status=$(grep "status" <<< "${summary}" | grep -Eo "enabled|disabled|unknown") + domains_being_blocked_raw=$(grep "domains_being_blocked" <<< "${summary}" | grep -Eo "[0-9]+$") domains_being_blocked=$(printf "%'.f" "${domains_being_blocked_raw}") @@ -412,42 +414,6 @@ GetNetworkInformation() { } GetPiholeInformation() { - # Get Pi-hole status - pihole_web_status=$(pihole status web) - - if [[ ${pihole_web_status} -ge 1 ]]; then - pihole_status="Active" - pihole_heatmap=${green_text} - pihole_check_box=${check_box_good} - elif [[ ${pihole_web_status} == 0 ]]; then - pihole_status="Offline" - pihole_heatmap=${red_text} - pihole_check_box=${check_box_bad} - pico_status=${pico_status_off} - mini_status_=${mini_status_off} - tiny_status_=${tiny_status_off} - full_status_=${full_status_off} - mega_status=${mega_status_off} - elif [[ ${pihole_web_status} == -1 ]]; then - pihole_status="DNS Offline" - pihole_heatmap=${red_text} - pihole_check_box=${check_box_bad} - pico_status=${pico_status_dns_down} - mini_status_=${mini_status_dns_down} - tiny_status_=${tiny_status_dns_down} - full_status_=${full_status_dns_down} - mega_status=${mega_status_dns_down} - else - pihole_status="Unknown" - pihole_heatmap=${yellow_text} - pihole_check_box=${check_box_question} - pico_status=${pico_status_unknown} - mini_status_=${mini_status_unknown} - tiny_status_=${tiny_status_unknown} - full_status_=${full_status_unknown} - mega_status=${mega_status_unknown} - fi - # Get FTL status ftlPID=$(pidof pihole-FTL) @@ -467,6 +433,48 @@ GetPiholeInformation() { ftl_cpu="$(ps -p "${ftlPID}" -o %cpu | tail -n1 | tr -d '[:space:]')" ftl_mem_percentage="$(ps -p "${ftlPID}" -o %mem | tail -n1 | tr -d '[:space:]')" fi + + # Get Pi-hole (blocking) status + ftl_dns_port=$(GetFTLData "dns-port") + + # ${ftl_dns_port} == 0 DNS server part of dnsmasq disabled, ${ftl_status} == "Not running" no ftlPID found + if [[ ${ftl_dns_port} == 0 ]] || [[ ${ftl_status} == "Not running" ]]; then + pihole_status="DNS Offline" + pihole_heatmap=${red_text} + pihole_check_box=${check_box_bad} + pico_status=${pico_status_dns_down} + mini_status_=${mini_status_dns_down} + tiny_status_=${tiny_status_dns_down} + full_status_=${full_status_dns_down} + mega_status=${mega_status_dns_down} + else + if [[ ${blocking_status} == "enabled" ]]; then + pihole_status="Active" + pihole_heatmap=${green_text} + pihole_check_box=${check_box_good} + fi + if [[ ${blocking_status} == "disabled" ]]; then + pihole_status="Blocking disabled" + pihole_heatmap=${red_text} + pihole_check_box=${check_box_bad} + pico_status=${pico_status_off} + mini_status_=${mini_status_off} + tiny_status_=${tiny_status_off} + full_status_=${full_status_off} + mega_status=${mega_status_off} + fi + if [[ ${blocking_status} == "unknown" ]]; then + pihole_status="Unknown" + pihole_heatmap=${yellow_text} + pihole_check_box=${check_box_question} + pico_status=${pico_status_unknown} + mini_status_=${mini_status_unknown} + tiny_status_=${tiny_status_unknown} + full_status_=${full_status_unknown} + mega_status=${mega_status_unknown} + fi + fi + } GetVersionInformation() { @@ -1206,9 +1214,9 @@ NormalPADD() { # Start getting our information GetVersionInformation ${padd_size} + GetSummaryInformation ${padd_size} GetPiholeInformation ${padd_size} GetNetworkInformation ${padd_size} - GetSummaryInformation ${padd_size} GetSystemInformation ${padd_size} # Sleep for 5 seconds From d23aa557f8a0db5179ae676ddd57461a37b51e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Thu, 6 Jan 2022 14:10:23 +0100 Subject: [PATCH 02/14] Use netcat to communicate with FTL's telnet API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/padd.sh b/padd.sh index d7081a60..7561a2cb 100755 --- a/padd.sh +++ b/padd.sh @@ -113,31 +113,14 @@ padd_logo_retro_3="${bold_text}${green_text}| ${red_text}/${yellow_text}-${gre ############################################# GETTERS ############################################## GetFTLData() { - local ftl_port LINE + local ftl_port + ftl_port=$(cat /run/pihole-FTL.port 2> /dev/null) if [[ -n "$ftl_port" ]]; then - # Open connection to FTL - exec 3<>"/dev/tcp/127.0.0.1/$ftl_port" - - # Test if connection is open - if { "true" >&3; } 2> /dev/null; then - # Send command to FTL and ask to quit when finished - echo -e ">$1 >quit" >&3 - - # Read input until we received an empty string and the connection is - # closed - read -r -t 1 LINE <&3 - until [[ -z "${LINE}" ]] && [[ ! -t 3 ]]; do - echo "$LINE" >&1 - read -r -t 1 LINE <&3 - done - - # Close connection - exec 3>&- - exec 3<&- - fi + # Send command to FTL and ask to quit when finished + echo ">$1 >quit" | nc 127.0.0.1 "${ftl_port}" else - echo "0" + echo "0" fi } From d7df5c57b5f134722d1e47311b8f8fb45a39607f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Fri, 25 Mar 2022 22:11:34 +0100 Subject: [PATCH 03/14] POSIXify part I MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 256 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 127 insertions(+), 129 deletions(-) diff --git a/padd.sh b/padd.sh index 688611f5..ace610e5 100755 --- a/padd.sh +++ b/padd.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/sh # shellcheck disable=SC1091 # PADD @@ -27,7 +27,7 @@ padd_version="v3.6.7" today=$(date +%Y%m%d) # CORES -declare -i core_count=1 +core_count=1 core_count=$(cat /sys/devices/system/cpu/kernel_max 2> /dev/null)+1 # Get Config variables @@ -113,10 +113,8 @@ padd_logo_retro_3="${bold_text}${green_text}| ${red_text}/${yellow_text}-${gre ############################################# GETTERS ############################################## GetFTLData() { - local ftl_port - ftl_port=$(cat /run/pihole-FTL.port 2> /dev/null) - if [[ -n "$ftl_port" ]]; then + if [ -n "$ftl_port" ]; then # Send command to FTL and ask to quit when finished echo ">$1 >quit" | nc 127.0.0.1 "${ftl_port}" else @@ -125,30 +123,28 @@ GetFTLData() { } GetSummaryInformation() { - local summary - local cache_info summary=$(GetFTLData "stats") cache_info=$(GetFTLData "cacheinfo") - clients=$(grep "unique_clients" <<< "${summary}" | grep -Eo "[0-9]+$") + clients=$(echo "${summary}" | grep "unique_clients" | grep -Eo "[0-9]+$") blocking_status=$(grep "status" <<< "${summary}" | grep -Eo "enabled|disabled|unknown") - domains_being_blocked_raw=$(grep "domains_being_blocked" <<< "${summary}" | grep -Eo "[0-9]+$") + domains_being_blocked_raw=$(echo "${summary}" | grep "domains_being_blocked" | grep -Eo "[0-9]+$") domains_being_blocked=$(printf "%'.f" "${domains_being_blocked_raw}") - dns_queries_today_raw=$(grep "dns_queries_today" <<< "$summary" | grep -Eo "[0-9]+$") + dns_queries_today_raw=$(echo "$summary" | grep "dns_queries_today" | grep -Eo "[0-9]+$") dns_queries_today=$(printf "%'.f" "${dns_queries_today_raw}") - ads_blocked_today_raw=$(grep "ads_blocked_today" <<< "$summary" | grep -Eo "[0-9]+$") + ads_blocked_today_raw=$(echo "$summary" | grep "ads_blocked_today" | grep -Eo "[0-9]+$") ads_blocked_today=$(printf "%'.f" "${ads_blocked_today_raw}") - ads_percentage_today_raw=$(grep "ads_percentage_today" <<< "$summary" | grep -Eo "[0-9.]+$") + ads_percentage_today_raw=$(echo "$summary" | grep "ads_percentage_today" | grep -Eo "[0-9.]+$") ads_percentage_today=$(printf "%'.1f" "${ads_percentage_today_raw}") - cache_size=$(grep "cache-size" <<< "$cache_info" | grep -Eo "[0-9.]+$") - cache_deletes=$(grep "cache-live-freed" <<< "$cache_info" | grep -Eo "[0-9.]+$") - cache_inserts=$(grep "cache-inserted" <<< "$cache_info" | grep -Eo "[0-9.]+$") + cache_size=$(echo "$cache_info" | grep "cache-size" | grep -Eo "[0-9.]+$") + cache_deletes=$(echo "$cache_info" | grep "cache-live-freed" | grep -Eo "[0-9.]+$") + cache_inserts=$(echo "$cache_info"| grep "cache-inserted" | grep -Eo "[0-9.]+$") latest_blocked=$(GetFTLData recentBlocked) @@ -156,11 +152,11 @@ GetSummaryInformation() { top_domain=$(GetFTLData "top-domains (1)" | awk '{print $3}') - read -r -a top_client_raw <<< "$(GetFTLData "top-clients (1)")" - if [[ "${top_client_raw[3]}" ]]; then - top_client="${top_client_raw[3]}" + top_client_raw=$(GetFTLData "top-clients (1)" | awk '{print $4}') + if [ -z "${top_client_raw}" ]; then + top_client=$(GetFTLData "top-clients (1)" | awk '{print $3}') else - top_client="${top_client_raw[2]}" + top_client="${top_client_raw}" fi if [ "$1" = "pico" ] || [ "$1" = "nano" ] || [ "$1" = "micro" ]; then @@ -193,7 +189,7 @@ GetSummaryInformation() { if [ ${#top_client} -gt 38 ]; then top_client=$(echo "$top_client" | cut -c1-38)"..." fi - elif [[ "$1" = "regular" || "$1" = "slim" ]]; then + elif [ "$1" = "regular" ] || [ "$1" = "slim" ]; then ads_blocked_bar=$(BarGenerator "$ads_percentage_today" 40 "color") else ads_blocked_bar=$(BarGenerator "$ads_percentage_today" 30 "color") @@ -210,17 +206,17 @@ GetSystemInformation() { # CPU temperature if [ -f /sys/class/thermal/thermal_zone0/temp ]; then - cpu=$( /dev/null)") # is PADD up-to-date? - if [[ "${padd_version_latest}" == "" ]]; then + if [ "${padd_version_latest}" = "" ]; then padd_version_heatmap=${yellow_text} else - if [[ "${padd_version}" != "${padd_version_latest}" ]]; then + if [ "${padd_version}" != "${padd_version_latest}" ]; then padd_out_of_date_flag="true" padd_version_heatmap=${red_text} else @@ -555,7 +553,7 @@ GetVersionInformation() { # was any portion of Pi-hole out-of-date? # yes, pi-hole is out of date - if [[ "${out_of_date_flag}" == "true" ]]; then + if [ "${out_of_date_flag}" = "true" ]; then version_status="Pi-hole is out-of-date!" version_heatmap=${red_text} version_check_box=${check_box_bad} @@ -566,7 +564,7 @@ GetVersionInformation() { mega_status=${mega_status_update} else # but is PADD out-of-date? - if [[ "${padd_out_of_date_flag}" == "true" ]]; then + if [ "${padd_out_of_date_flag}" = "true" ]; then version_status="PADD is out-of-date!" version_heatmap=${red_text} version_check_box=${check_box_bad} @@ -629,7 +627,7 @@ ceol=$(tput el) # wrapper - echo with a clear eol afterwards to wipe any artifacts remaining from last print CleanEcho() { - echo -e "${ceol}$1" + echo "${ceol}$1" } # wrapper - printf @@ -659,7 +657,7 @@ PrintLogo() { CleanEcho "${padd_text}${dim_text}slim${reset_text} ${full_status_}" CleanEcho "" # For the next two, use printf to make sure spaces aren't collapsed - elif [[ "$1" = "regular" || "$1" = "slim" ]]; then + elif [ "$1" = "regular" ] || [ "$1" = "slim" ]; then CleanPrintf "${padd_logo_1}\e[0K\\n" CleanPrintf "${padd_logo_2}Pi-hole® ${core_version_heatmap}v${core_version}${reset_text}, Web ${web_version_heatmap}v${web_version}${reset_text}, FTL ${ftl_version_heatmap}v${ftl_version}${reset_text}\e[0K\\n" CleanPrintf "${padd_logo_3}PADD ${padd_version_heatmap}${padd_version}${reset_text} ${full_status_}${reset_text}\e[0K\\n" @@ -696,7 +694,7 @@ PrintNetworkInformation() { CleanPrintf " %-9s%-19s\e[0K\\n" "IP:" "${pi_ip4_addr}" CleanPrintf " %-9s%-10s\e[0K\\n" "DNS:" "${dns_information}" - if [[ "${DHCP_ACTIVE}" == "true" ]]; then + if [ "${DHCP_ACTIVE}" = "true" ]; then CleanPrintf " %-9s${dhcp_heatmap}%-10s${reset_text} %-9s${dhcp_ipv6_heatmap}%-10s${reset_text}\e[0K\\n" "DHCP:" "${dhcp_status}" "IPv6:" ${dhcp_ipv6_status} fi elif [ "$1" = "tiny" ]; then @@ -705,17 +703,17 @@ PrintNetworkInformation() { CleanPrintf " %-6s%-39s\e[0K\\n" "IPv6:" "${pi_ip6_addr}" CleanPrintf " %-10s%-16s %-8s%-16s\e[0K\\n" "DNS:" "${dns_information}" "DNSSEC:" "${dnssec_heatmap}${dnssec_status}${reset_text}" - if [[ "${DHCP_ACTIVE}" == "true" ]]; then + if [ "${DHCP_ACTIVE}" = "true" ]; then CleanPrintf " %-10s${dhcp_heatmap}%-16s${reset_text} %-8s${dhcp_ipv6_heatmap}%-10s${reset_text}\e[0K\\n" "DHCP:" "${dhcp_status}" "IPv6:" ${dhcp_ipv6_status} CleanPrintf "%s\e[0K\\n" "${dhcp_info}" fi - elif [[ "$1" = "regular" || "$1" = "slim" ]]; then + elif [ "$1" = "regular" ] || [ "$1" = "slim" ]; then CleanEcho "${bold_text}NETWORK ===================================================${reset_text}" CleanPrintf " %-10s%-19s %-10s%-19s\e[0K\\n" "Hostname:" "${full_hostname}" "IP:" "${pi_ip4_addr}" CleanPrintf " %-6s%-19s\e[0K\\n" "IPv6:" "${pi_ip6_addr}" CleanPrintf " %-10s%-19s %-10s%-19s\e[0K\\n" "DNS:" "${dns_information}" "DNSSEC:" "${dnssec_heatmap}${dnssec_status}${reset_text}" - if [[ "${DHCP_ACTIVE}" == "true" ]]; then + if [ "${DHCP_ACTIVE}" = "true" ]; then CleanPrintf " %-10s${dhcp_heatmap}%-19s${reset_text} %-10s${dhcp_ipv6_heatmap}%-19s${reset_text}\e[0K\\n" "DHCP:" "${dhcp_status}" "IPv6:" ${dhcp_ipv6_status} CleanPrintf "%s\e[0K\\n" "${dhcp_info}" fi @@ -749,7 +747,7 @@ PrintPiholeInformation() { elif [ "$1" = "tiny" ]; then CleanEcho "${bold_text}PI-HOLE ============================================${reset_text}" CleanPrintf " %-10s${pihole_heatmap}%-16s${reset_text} %-8s${ftl_heatmap}%-10s${reset_text}\e[0K\\n" "Status:" "${pihole_status}" "FTL:" "${ftl_status}" - elif [[ "$1" = "regular" || "$1" = "slim" ]]; then + elif [ "$1" = "regular" ] || [ "$1" = "slim" ]; then CleanEcho "${bold_text}PI-HOLE ===================================================${reset_text}" CleanPrintf " %-10s${pihole_heatmap}%-19s${reset_text} %-10s${ftl_heatmap}%-19s${reset_text}\e[0K\\n" "Status:" "${pihole_status}" "FTL:" "${ftl_status}" else @@ -777,7 +775,7 @@ PrintPiholeStats() { CleanPrintf " %-9s[%-20s] %-5s\e[0K\\n" "Piholed:" "${ads_blocked_bar}" "${ads_percentage_today}%" CleanPrintf " %-9s%-29s\e[0K\\n" "Piholed:" "${ads_blocked_today} out of ${dns_queries_today}" CleanPrintf " %-9s%-29s\e[0K\\n" "Latest:" "${latest_blocked}" - if [[ "${DHCP_ACTIVE}" != "true" ]]; then + if [ "${DHCP_ACTIVE}" != "true" ]; then CleanPrintf " %-9s%-29s\\n" "Top Ad:" "${top_blocked}" fi elif [ "$1" = "tiny" ]; then @@ -787,18 +785,18 @@ PrintPiholeStats() { CleanPrintf " %-10s%-39s\e[0K\\n" "Pi-holed:" "${ads_blocked_today} out of ${dns_queries_today}" CleanPrintf " %-10s%-39s\e[0K\\n" "Latest:" "${latest_blocked}" CleanPrintf " %-10s%-39s\e[0K\\n" "Top Ad:" "${top_blocked}" - if [[ "${DHCP_ACTIVE}" != "true" ]]; then + if [ "${DHCP_ACTIVE}" != "true" ]; then CleanPrintf " %-10s%-39s\e[0K\\n" "Top Dmn:" "${top_domain}" CleanPrintf " %-10s%-39s\e[0K\\n" "Top Clnt:" "${top_client}" fi - elif [[ "$1" = "regular" || "$1" = "slim" ]]; then + elif [ "$1" = "regular" ] || [ "$1" = "slim" ]; then CleanEcho "${bold_text}STATS =====================================================${reset_text}" CleanPrintf " %-10s%-49s\e[0K\\n" "Blocking:" "${domains_being_blocked} domains" CleanPrintf " %-10s[%-40s] %-5s\e[0K\\n" "Pi-holed:" "${ads_blocked_bar}" "${ads_percentage_today}%" CleanPrintf " %-10s%-49s\e[0K\\n" "Pi-holed:" "${ads_blocked_today} out of ${dns_queries_today} queries" CleanPrintf " %-10s%-39s\e[0K\\n" "Latest:" "${latest_blocked}" CleanPrintf " %-10s%-39s\e[0K\\n" "Top Ad:" "${top_blocked}" - if [[ "${DHCP_ACTIVE}" != "true" ]]; then + if [ "${DHCP_ACTIVE}" != "true" ]; then CleanPrintf " %-10s%-39s\e[0K\\n" "Top Dmn:" "${top_domain}" CleanPrintf " %-10s%-39s\e[0K\\n" "Top Clnt:" "${top_client}" fi @@ -819,36 +817,36 @@ PrintPiholeStats() { PrintSystemInformation() { if [ "$1" = "pico" ]; then CleanEcho "${bold_text}CPU ================${reset_text}" - echo -ne "${ceol} [${cpu_load_1_heatmap}${cpu_bar}${reset_text}] ${cpu_percent}%" + printf "%b" "${ceol} [${cpu_load_1_heatmap}${cpu_bar}${reset_text}] ${cpu_percent}%" elif [ "$1" = "nano" ]; then CleanEcho "${ceol}${bold_text}SYSTEM =================${reset_text}" CleanEcho " Up: ${system_uptime}" - echo -ne "${ceol} CPU: [${cpu_load_1_heatmap}${cpu_bar}${reset_text}] ${cpu_percent}%" + printf "%b" "${ceol} CPU: [${cpu_load_1_heatmap}${cpu_bar}${reset_text}] ${cpu_percent}%" elif [ "$1" = "micro" ]; then CleanEcho "${bold_text}SYSTEM =======================${reset_text}" CleanEcho " Uptime: ${system_uptime}" CleanEcho " Load: [${cpu_load_1_heatmap}${cpu_bar}${reset_text}] ${cpu_percent}%" - echo -ne "${ceol} Memory: [${memory_heatmap}${memory_bar}${reset_text}] ${memory_percent}%" + printf "%b" "${ceol} Memory: [${memory_heatmap}${memory_bar}${reset_text}] ${memory_percent}%" elif [ "$1" = "mini" ]; then CleanEcho "${bold_text}SYSTEM =================================${reset_text}" CleanPrintf " %-9s%-29s\\n" "Uptime:" "${system_uptime}" CleanEcho " Load: [${cpu_load_1_heatmap}${cpu_bar}${reset_text}] ${cpu_percent}%" - echo -ne "${ceol} Memory: [${memory_heatmap}${memory_bar}${reset_text}] ${memory_percent}%" + printf "%b" "${ceol} Memory: [${memory_heatmap}${memory_bar}${reset_text}] ${memory_percent}%" elif [ "$1" = "tiny" ]; then CleanEcho "${bold_text}SYSTEM =============================================${reset_text}" CleanPrintf " %-10s%-29s\e[0K\\n" "Uptime:" "${system_uptime}" - CleanPrintf " %-10s${temp_heatmap}%-17s${reset_text} %-8s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-4s${reset_text}\e[0K\\n" "CPU Temp:" "${temperature}" "Load:" "${cpu_load[0]}" "${cpu_load[1]}" "${cpu_load[2]}" + CleanPrintf " %-10s${temp_heatmap}%-17s${reset_text} %-8s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-4s${reset_text}\e[0K\\n" "CPU Temp:" "${temperature}" "Load:" "${cpu_load_1}" "${cpu_load_5}" "${cpu_load_15}" # Memory and CPU bar CleanPrintf " %-10s[${memory_heatmap}%-7s${reset_text}] %-6s %-8s[${cpu_load_1_heatmap}%-7s${reset_text}] %-5s" "Memory:" "${memory_bar}" "${memory_percent}%" "CPU:" "${cpu_bar}" "${cpu_percent}%" # else we're not - elif [[ "$1" = "regular" || "$1" = "slim" ]]; then + elif [ "$1" = "regular" ] || [ "$1" = "slim" ]; then CleanEcho "${bold_text}SYSTEM ====================================================${reset_text}" # Uptime CleanPrintf " %-10s%-39s\e[0K\\n" "Uptime:" "${system_uptime}" # Temp and Loads CleanPrintf " %-10s${temp_heatmap}%-20s${reset_text}" "CPU Temp:" "${temperature}" - CleanPrintf " %-10s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-4s${reset_text}\e[0K\\n" "CPU Load:" "${cpu_load[0]}" "${cpu_load[1]}" "${cpu_load[2]}" + CleanPrintf " %-10s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-4s${reset_text}\e[0K\\n" "CPU Load:" "${cpu_load_1}" "${cpu_load_5]}" "${cpu_load_15}" # Memory and CPU bar CleanPrintf " %-10s[${memory_heatmap}%-10s${reset_text}] %-6s %-10s[${cpu_load_1_heatmap}%-10s${reset_text}] %-5s" "Memory:" "${memory_bar}" "${memory_percent}%" "CPU Load:" "${cpu_bar}" "${cpu_percent}%" @@ -858,7 +856,7 @@ PrintSystemInformation() { CleanPrintf " %-10s%-39s %-10s[${memory_heatmap}%-10s${reset_text}] %-6s\\n" "Uptime:" "${system_uptime}" "Memory:" "${memory_bar}" "${memory_percent}%" # CPU temp, load, percentage - CleanPrintf " %-10s${temp_heatmap}%-10s${reset_text} %-10s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-7s${reset_text} %-10s[${memory_heatmap}%-10s${reset_text}] %-6s" "CPU Temp:" "${temperature}" "CPU Load:" "${cpu_load[0]}" "${cpu_load[1]}" "${cpu_load[2]}" "CPU Load:" "${cpu_bar}" "${cpu_percent}%" + CleanPrintf " %-10s${temp_heatmap}%-10s${reset_text} %-10s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-7s${reset_text} %-10s[${memory_heatmap}%-10s${reset_text}] %-6s" "CPU Temp:" "${temperature}" "CPU Load:" "${cpu_load_1}" "${cpu_load_5}" "${cpu_load_15}" "CPU Load:" "${cpu_bar}" "${cpu_percent}%" fi } @@ -897,7 +895,7 @@ HeatmapGenerator () { BarGenerator() { # number of filled in cells in the bar barNumber=$(printf %.f "$(echo "$1 $2" | awk '{print ($1 / 100) * $2}')") - frontFill=$(for i in $(seq "$barNumber"); do echo -n '■'; done) + frontFill=$(for i in $(seq "$barNumber"); do printf "%b" "■"; done) # remaining "unfilled" cells in the bar backfillNumber=$(($2-barNumber)) @@ -907,17 +905,17 @@ BarGenerator() { # if the bar should be colored if [ "$3" = "color" ]; then # fill the rest in color - backFill=$(for i in $(seq $backfillNumber); do echo -n '■'; done) + backFill=$(for i in $(seq $backfillNumber); do printf "%b" "■"; done) out="${red_text}${frontFill}${green_text}${backFill}${reset_text}" # else, it shouldn't be colored in else # fill the rest with "space" - backFill=$(for i in $(seq $backfillNumber); do echo -n '·'; done) + backFill=$(for i in $(seq $backfillNumber); do printf "%b" "·"; done) out="${frontFill}${reset_text}${backFill}" fi # else, fill it all the way else - out=$(for i in $(seq "$2"); do echo -n '■'; done) + out=$(for i in $(seq "$2"); do printf "%b" "■"; done) fi echo "$out" @@ -926,29 +924,29 @@ BarGenerator() { # Checks the size of the screen and sets the value of padd_size SizeChecker(){ # Below Pico. Gives you nothing... - if [[ "$console_width" -lt "20" || "$console_height" -lt "10" ]]; then + if [ "$console_width" -lt "20" ] || [ "$console_height" -lt "10" ]; then # Nothing is this small, sorry clear - echo -e "${check_box_bad} Error!\\n PADD isn't\\n for ants!" + printf "%b" "${check_box_bad} Error!\\n PADD isn't\\n for ants!" exit 1 # Below Nano. Gives you Pico. - elif [[ "$console_width" -lt "24" || "$console_height" -lt "12" ]]; then + elif [ "$console_width" -lt "24" ] || [ "$console_height" -lt "12" ]; then padd_size="pico" # Below Micro, Gives you Nano. - elif [[ "$console_width" -lt "30" || "$console_height" -lt "16" ]]; then + elif [ "$console_width" -lt "30" ] || [ "$console_height" -lt "16" ]; then padd_size="nano" # Below Mini. Gives you Micro. - elif [[ "$console_width" -lt "40" || "$console_height" -lt "18" ]]; then + elif [ "$console_width" -lt "40" ] || [ "$console_height" -lt "18" ]; then padd_size="micro" # Below Tiny. Gives you Mini. - elif [[ "$console_width" -lt "53" || "$console_height" -lt "20" ]]; then + elif [ "$console_width" -lt "53" ] || [ "$console_height" -lt "20" ]; then padd_size="mini" # Below Slim. Gives you Tiny. - elif [[ "$console_width" -lt "60" || "$console_height" -lt "21" ]]; then + elif [ "$console_width" -lt "60" ] || [ "$console_height" -lt "21" ]; then padd_size="tiny" # Below Regular. Gives you Slim. - elif [[ "$console_width" -lt "80" || "$console_height" -lt "26" ]]; then - if [[ "$console_height" -lt "22" ]]; then + elif [ "$console_width" -lt "80" ] || [ "$console_height" -lt "26" ]; then + if [ "$console_height" -lt "22" ]; then padd_size="slim" else padd_size="regular" @@ -985,11 +983,11 @@ CheckConnectivity() { # echo "$wait_timer = $inner_wait_timer" while [ $inner_wait_timer -gt 0 ]; do if [ "$1" = "pico" ] || [ "$1" = "nano" ] || [ "$1" = "micro" ]; then - echo -ne "Attempt #${connection_attempts} failed...\\r" + printf "%b" "Attempt #${connection_attempts} failed...\\r" elif [ "$1" = "mini" ] || [ "$1" = "tiny" ]; then - echo -ne "- Attempt ${connection_attempts} failed, wait ${inner_wait_timer} \\r" + printf "%b" "- Attempt ${connection_attempts} failed, wait ${inner_wait_timer} \\r" else - echo -ne " - Attempt ${connection_attempts} failed... waiting ${inner_wait_timer} seconds... \\r" + printf "%b" " - Attempt ${connection_attempts} failed... waiting ${inner_wait_timer} seconds... \\r" fi sleep 1 inner_wait_timer=$((inner_wait_timer-1)) @@ -1023,13 +1021,13 @@ CheckConnectivity() { # Credit: https://stackoverflow.com/a/46324904 json_extract() { - local key=$1 - local json=$2 + key=$1 + json=$2 - local string_regex='"([^"\]|\\.)*"' - local number_regex='-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?' - local value_regex="${string_regex}|${number_regex}|true|false|null" - local pair_regex="\"${key}\"[[:space:]]*:[[:space:]]*(${value_regex})" + string_regex='"([^"\]|\\.)*"' + number_regex='-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?' + value_regex="${string_regex}|${number_regex}|true|false|null" + pair_regex="\"${key}\"[[:space:]]*:[[:space:]]*(${value_regex})" if [[ ${json} =~ ${pair_regex} ]]; then # remove leading and trailing quotes @@ -1049,38 +1047,38 @@ OutputJSON() { StartupRoutine(){ if [ "$1" = "pico" ] || [ "$1" = "nano" ] || [ "$1" = "micro" ]; then PrintLogo "$1" - echo -e "START-UP ===========" - echo -e "Checking connection." + printf "%b" "START-UP ===========" + printf "%b" "Checking connection." CheckConnectivity "$1" - echo -e "Starting PADD..." + printf "%b" "Starting PADD..." # Get PID of PADD pid=$$ - echo -ne " [■·········] 10%\\r" + printf "%b" " [■·········] 10%\\r" echo ${pid} > ./PADD.pid # Check for updates - echo -ne " [■■········] 20%\\r" + printf "%b" " [■■········] 20%\\r" if [ -e "piHoleVersion" ]; then rm -f piHoleVersion - echo -ne " [■■■·······] 30%\\r" + printf "%b" " [■■■·······] 30%\\r" else - echo -ne " [■■■·······] 30%\\r" + printf "%b" " [■■■·······] 30%\\r" fi # Get our information for the first time - echo -ne " [■■■■······] 40%\\r" + printf "%b" " [■■■■······] 40%\\r" GetSystemInformation "$1" - echo -ne " [■■■■■·····] 50%\\r" + printf "%b" " [■■■■■·····] 50%\\r" GetSummaryInformation "$1" - echo -ne " [■■■■■■····] 60%\\r" + printf "%b" " [■■■■■■····] 60%\\r" GetPiholeInformation "$1" - echo -ne " [■■■■■■■···] 70%\\r" + printf "%b" " [■■■■■■■···] 70%\\r" GetNetworkInformation "$1" - echo -ne " [■■■■■■■■··] 80%\\r" + printf "%b" " [■■■■■■■■··] 80%\\r" GetVersionInformation "$1" - echo -ne " [■■■■■■■■■·] 90%\\r" - echo -ne " [■■■■■■■■■■] 100%\\n" + printf "%b" " [■■■■■■■■■·] 90%\\r" + printf "%b" " [■■■■■■■■■■] 100%\\n" elif [ "$1" = "mini" ]; then PrintLogo "$1" @@ -1117,16 +1115,16 @@ StartupRoutine(){ echo " - $version_status" else - echo -e "${padd_logo_retro_1}" - echo -e "${padd_logo_retro_2}Pi-hole® Ad Detection Display" - echo -e "${padd_logo_retro_3}A client for Pi-hole\\n" + printf "%b" "${padd_logo_retro_1}\\n" + printf "%b" "${padd_logo_retro_2}Pi-hole® Ad Detection Display\\n" + printf "%b" "${padd_logo_retro_3}A client for Pi-hole\\n" if [ "$1" = "tiny" ]; then echo "START UP ============================================" else echo "START UP ===================================================" fi - echo -e "- Checking internet connection..." + printf "%b" "- Checking internet connection..." CheckConnectivity "$1" # Get PID of PADD @@ -1170,7 +1168,7 @@ StartupRoutine(){ } NormalPADD() { - for (( ; ; )); do + while true; do console_width=$(tput cols) console_height=$(tput lines) @@ -1220,7 +1218,7 @@ EOM exit 0 } -if [[ $# = 0 ]]; then +if [ $# = 0 ]; then # Turns off the cursor # (From Pull request #8 https://github.com/jpmck/PADD/pull/8) setterm -cursor off From a9b1f1b5d540f324e055ceae966a70874032059e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 27 Mar 2022 19:24:22 +0200 Subject: [PATCH 04/14] POSIXify part II MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/padd.sh b/padd.sh index ace610e5..03c5cb9d 100755 --- a/padd.sh +++ b/padd.sh @@ -478,16 +478,15 @@ GetVersionInformation() { else # the file doesn't exist, create it... # Gather core version information... - read -r -a core_versions <<< "$(pihole -v -p)" - core_version=$(echo "${core_versions[3]}" | tr -d '\r\n[:alpha:]') - core_version_latest=${core_versions[5]//)} + core_version=$(pihole -v -p | awk '{print $4}' | tr -d '[:alpha:]') + core_version_latest=$(pihole -v -p | awk '{print $6}' | tr -d ')') if [ "${core_version_latest}" = "ERROR" ]; then core_version_heatmap=${yellow_text} else core_version_latest=$(echo "${core_version_latest}" | tr -d '\r\n[:alpha:]') # is core up-to-date? - if [[ "${core_version}" != "${core_version_latest}" ]]; then + if [ "${core_version}" != "${core_version_latest}" ]; then out_of_date_flag="true" core_version_heatmap=${red_text} else @@ -497,9 +496,8 @@ GetVersionInformation() { # Gather web version information... if [ "$INSTALL_WEB_INTERFACE" = true ]; then - read -r -a web_versions <<< "$(pihole -v -a)" - web_version=$(echo "${web_versions[3]}" | tr -d '\r\n[:alpha:]') - web_version_latest=${web_versions[5]//)} + web_version=$(pihole -v -a | awk '{print $4}' | tr -d '[:alpha:]') + web_version_latest=$(pihole -v -a | awk '{print $6}' | tr -d ')') if [ "${web_version_latest}" = "ERROR" ]; then web_version_heatmap=${yellow_text} else @@ -520,10 +518,9 @@ GetVersionInformation() { fi # Gather FTL version information... - read -r -a ftl_versions <<< "$(pihole -v -f)" - ftl_version=$(echo "${ftl_versions[3]}" | tr -d '\r\n[:alpha:]') - ftl_version_latest=${ftl_versions[5]//)} - if [ "${ftl_version_latest}" == "ERROR" ]; then + ftl_version=$(pihole -v -f | awk '{print $4}' | tr -d '[:alpha:]') + ftl_version_latest=$(pihole -v -f | awk '{print $6}' | tr -d ')') + if [ "${ftl_version_latest}" = "ERROR" ]; then ftl_version_heatmap=${yellow_text} else ftl_version_latest=$(echo "${ftl_version_latest}" | tr -d '\r\n[:alpha:]') @@ -537,8 +534,7 @@ GetVersionInformation() { fi # PADD version information... - padd_version_latest=$(json_extract tag_name "$(curl -s 'https://api.github.com/repos/pi-hole/PADD/releases/latest' 2> /dev/null)") - + padd_version_latest="$(curl --silent https://api.github.com/repos/pi-hole/PADD/releases/latest | grep '"tag_name":' | awk -F \" '{print $4}')" # is PADD up-to-date? if [ "${padd_version_latest}" = "" ]; then padd_version_heatmap=${yellow_text} @@ -1019,24 +1015,6 @@ CheckConnectivity() { fi } -# Credit: https://stackoverflow.com/a/46324904 -json_extract() { - key=$1 - json=$2 - - string_regex='"([^"\]|\\.)*"' - number_regex='-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?' - value_regex="${string_regex}|${number_regex}|true|false|null" - pair_regex="\"${key}\"[[:space:]]*:[[:space:]]*(${value_regex})" - - if [[ ${json} =~ ${pair_regex} ]]; then - # remove leading and trailing quotes - sed -e 's/^"//' -e 's/"$//' <<<"${BASH_REMATCH[1]}" - else - return 1 - fi -} - ########################################## MAIN FUNCTIONS ########################################## OutputJSON() { @@ -1222,7 +1200,7 @@ if [ $# = 0 ]; then # Turns off the cursor # (From Pull request #8 https://github.com/jpmck/PADD/pull/8) setterm -cursor off - trap "{ setterm -cursor on ; echo "" ; exit 0 ; }" SIGINT SIGTERM EXIT + trap "{ setterm -cursor on ; echo "" ; exit 0 ; }" INT TERM EXIT clear From fe52b85fd9e10d836529cb71d39f4b0878dd6108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Mon, 28 Mar 2022 21:38:03 +0200 Subject: [PATCH 05/14] POSIXify part III MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/padd.sh b/padd.sh index 03c5cb9d..7765d892 100755 --- a/padd.sh +++ b/padd.sh @@ -33,6 +33,8 @@ core_count=$(cat /sys/devices/system/cpu/kernel_max 2> /dev/null)+1 # Get Config variables . /etc/pihole/setupVars.conf +piHoleVersion="./piHoleVersion" + # COLORS CSI="$(printf '\033')[" red_text="${CSI}91m" # Red @@ -131,16 +133,16 @@ GetSummaryInformation() { blocking_status=$(grep "status" <<< "${summary}" | grep -Eo "enabled|disabled|unknown") domains_being_blocked_raw=$(echo "${summary}" | grep "domains_being_blocked" | grep -Eo "[0-9]+$") - domains_being_blocked=$(printf "%'.f" "${domains_being_blocked_raw}") + domains_being_blocked=$(printf "%.f" "${domains_being_blocked_raw}") dns_queries_today_raw=$(echo "$summary" | grep "dns_queries_today" | grep -Eo "[0-9]+$") - dns_queries_today=$(printf "%'.f" "${dns_queries_today_raw}") + dns_queries_today=$(printf "%.f" "${dns_queries_today_raw}") ads_blocked_today_raw=$(echo "$summary" | grep "ads_blocked_today" | grep -Eo "[0-9]+$") - ads_blocked_today=$(printf "%'.f" "${ads_blocked_today_raw}") + ads_blocked_today=$(printf "%.f" "${ads_blocked_today_raw}") ads_percentage_today_raw=$(echo "$summary" | grep "ads_percentage_today" | grep -Eo "[0-9.]+$") - ads_percentage_today=$(printf "%'.1f" "${ads_percentage_today_raw}") + ads_percentage_today=$(printf "%.1f" "${ads_percentage_today_raw}") cache_size=$(echo "$cache_info" | grep "cache-size" | grep -Eo "[0-9.]+$") cache_deletes=$(echo "$cache_info" | grep "cache-live-freed" | grep -Eo "[0-9.]+$") @@ -460,12 +462,12 @@ GetPiholeInformation() { GetVersionInformation() { # Check if version status has been saved - if [ -e "piHoleVersion" ]; then # the file exists... + if [ -f "$piHoleVersion" ]; then # the file exists... # the file exits, use it # shellcheck disable=SC1091 - . piHoleVersion + . "$piHoleVersion" - # Today is... + # Today is today=$(date +%Y%m%d) # was the last check today? @@ -473,7 +475,7 @@ GetVersionInformation() { # shellcheck disable=SC2154 if [ "${today}" != "${last_check}" ]; then # no, it wasn't today # Remove the Pi-hole version file... - rm -f piHoleVersion + rm -f "$piHoleVersion" fi else # the file doesn't exist, create it... @@ -583,7 +585,7 @@ GetVersionInformation() { fi # write it all to the file - echo "last_check=${today}" > ./piHoleVersion + echo "last_check=${today}" > "$piHoleVersion" { echo "core_version=$core_version" echo "core_version_latest=$core_version_latest" @@ -610,7 +612,7 @@ GetVersionInformation() { echo "tiny_status_=\"$tiny_status_\"" echo "full_status_=\"$full_status_\"" echo "mega_status=\"$mega_status\"" - } >> ./piHoleVersion + } >> "$piHoleVersion" # there's a file now fi @@ -842,7 +844,7 @@ PrintSystemInformation() { # Temp and Loads CleanPrintf " %-10s${temp_heatmap}%-20s${reset_text}" "CPU Temp:" "${temperature}" - CleanPrintf " %-10s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-4s${reset_text}\e[0K\\n" "CPU Load:" "${cpu_load_1}" "${cpu_load_5]}" "${cpu_load_15}" + CleanPrintf " %-10s${cpu_load_1_heatmap}%-4s${reset_text}, ${cpu_load_5_heatmap}%-4s${reset_text}, ${cpu_load_15_heatmap}%-4s${reset_text}\e[0K\\n" "CPU Load:" "${cpu_load_1}" "${cpu_load_5}" "${cpu_load_15}" # Memory and CPU bar CleanPrintf " %-10s[${memory_heatmap}%-10s${reset_text}] %-6s %-10s[${cpu_load_1_heatmap}%-10s${reset_text}] %-5s" "Memory:" "${memory_bar}" "${memory_percent}%" "CPU Load:" "${cpu_bar}" "${cpu_percent}%" @@ -1037,8 +1039,8 @@ StartupRoutine(){ # Check for updates printf "%b" " [■■········] 20%\\r" - if [ -e "piHoleVersion" ]; then - rm -f piHoleVersion + if [ -f "$piHoleVersion" ]; then + rm -f "$piHoleVersion" printf "%b" " [■■■·······] 30%\\r" else printf "%b" " [■■■·······] 30%\\r" @@ -1072,9 +1074,9 @@ StartupRoutine(){ # Check for updates echo "- Checking for version file." - if [ -e "piHoleVersion" ]; then + if [ -f "$piHoleVersion" ]; then echo " - Found and deleted." - rm -f piHoleVersion + rm -f "$piHoleVersion" else echo " - Not found." fi @@ -1112,9 +1114,9 @@ StartupRoutine(){ # Check for updates echo "- Checking for PADD version file..." - if [ -e "piHoleVersion" ]; then + if [ -f "$piHoleVersion" ]; then echo " - PADD version file found... deleting." - rm -f piHoleVersion + rm -f "$piHoleVersion" else echo " - PADD version file not found." fi From 2b5cf846f3e00356376e8a40154201238185360c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Tue, 29 Mar 2022 21:50:36 +0200 Subject: [PATCH 06/14] POSIXify part IV MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/padd.sh b/padd.sh index 7765d892..aae5fd04 100755 --- a/padd.sh +++ b/padd.sh @@ -33,7 +33,7 @@ core_count=$(cat /sys/devices/system/cpu/kernel_max 2> /dev/null)+1 # Get Config variables . /etc/pihole/setupVars.conf -piHoleVersion="./piHoleVersion" +piHoleVersion_file="./piHoleVersion" # COLORS CSI="$(printf '\033')[" @@ -462,10 +462,10 @@ GetPiholeInformation() { GetVersionInformation() { # Check if version status has been saved - if [ -f "$piHoleVersion" ]; then # the file exists... + if [ -f "$piHoleVersion_file" ]; then # the file exists... # the file exits, use it - # shellcheck disable=SC1091 - . "$piHoleVersion" + # shellcheck source=./piHoleVersion + . "$piHoleVersion_file" # Today is today=$(date +%Y%m%d) @@ -475,7 +475,7 @@ GetVersionInformation() { # shellcheck disable=SC2154 if [ "${today}" != "${last_check}" ]; then # no, it wasn't today # Remove the Pi-hole version file... - rm -f "$piHoleVersion" + rm -f "$piHoleVersion_file" fi else # the file doesn't exist, create it... @@ -585,7 +585,7 @@ GetVersionInformation() { fi # write it all to the file - echo "last_check=${today}" > "$piHoleVersion" + echo "last_check=${today}" > "$piHoleVersion_file" { echo "core_version=$core_version" echo "core_version_latest=$core_version_latest" @@ -612,7 +612,7 @@ GetVersionInformation() { echo "tiny_status_=\"$tiny_status_\"" echo "full_status_=\"$full_status_\"" echo "mega_status=\"$mega_status\"" - } >> "$piHoleVersion" + } >> "$piHoleVersion_file" # there's a file now fi @@ -925,7 +925,7 @@ SizeChecker(){ if [ "$console_width" -lt "20" ] || [ "$console_height" -lt "10" ]; then # Nothing is this small, sorry clear - printf "%b" "${check_box_bad} Error!\\n PADD isn't\\n for ants!" + printf "%b" "${check_box_bad} Error!\\n PADD isn't\\n for ants!\n" exit 1 # Below Nano. Gives you Pico. elif [ "$console_width" -lt "24" ] || [ "$console_height" -lt "12" ]; then @@ -1027,10 +1027,10 @@ OutputJSON() { StartupRoutine(){ if [ "$1" = "pico" ] || [ "$1" = "nano" ] || [ "$1" = "micro" ]; then PrintLogo "$1" - printf "%b" "START-UP ===========" - printf "%b" "Checking connection." + printf "%b" "START-UP ===========\n" + printf "%b" "Checking connection.\n" CheckConnectivity "$1" - printf "%b" "Starting PADD..." + printf "%b" "Starting PADD...\n" # Get PID of PADD pid=$$ @@ -1039,8 +1039,8 @@ StartupRoutine(){ # Check for updates printf "%b" " [■■········] 20%\\r" - if [ -f "$piHoleVersion" ]; then - rm -f "$piHoleVersion" + if [ -f "$piHoleVersion_file" ]; then + rm -f "$piHoleVersion_file" printf "%b" " [■■■·······] 30%\\r" else printf "%b" " [■■■·······] 30%\\r" @@ -1074,9 +1074,9 @@ StartupRoutine(){ # Check for updates echo "- Checking for version file." - if [ -f "$piHoleVersion" ]; then + if [ -f "$piHoleVersion_file" ]; then echo " - Found and deleted." - rm -f "$piHoleVersion" + rm -f "$piHoleVersion_file" else echo " - Not found." fi @@ -1095,16 +1095,16 @@ StartupRoutine(){ echo " - $version_status" else - printf "%b" "${padd_logo_retro_1}\\n" - printf "%b" "${padd_logo_retro_2}Pi-hole® Ad Detection Display\\n" - printf "%b" "${padd_logo_retro_3}A client for Pi-hole\\n" + printf "%b" "${padd_logo_retro_1}\n" + printf "%b" "${padd_logo_retro_2}Pi-hole® Ad Detection Display\n" + printf "%b" "${padd_logo_retro_3}A client for Pi-hole\n\n" if [ "$1" = "tiny" ]; then echo "START UP ============================================" else echo "START UP ===================================================" fi - printf "%b" "- Checking internet connection..." + printf "%b" "- Checking internet connection...\n" CheckConnectivity "$1" # Get PID of PADD @@ -1114,9 +1114,9 @@ StartupRoutine(){ # Check for updates echo "- Checking for PADD version file..." - if [ -f "$piHoleVersion" ]; then + if [ -f "$piHoleVersion_file" ]; then echo " - PADD version file found... deleting." - rm -f "$piHoleVersion" + rm -f "$piHoleVersion_file" else echo " - PADD version file not found." fi From f6818f058cb733f3243ffc7c65b6acf1d84a7476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Thu, 7 Apr 2022 10:45:45 +0200 Subject: [PATCH 07/14] POSIXify latest development MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/padd.sh b/padd.sh index aae5fd04..11338d01 100755 --- a/padd.sh +++ b/padd.sh @@ -130,8 +130,8 @@ GetSummaryInformation() { clients=$(echo "${summary}" | grep "unique_clients" | grep -Eo "[0-9]+$") - blocking_status=$(grep "status" <<< "${summary}" | grep -Eo "enabled|disabled|unknown") - + blocking_status=$(echo "${summary}" | grep "status" | grep -Eo "enabled|disabled|unknown" ) + domains_being_blocked_raw=$(echo "${summary}" | grep "domains_being_blocked" | grep -Eo "[0-9]+$") domains_being_blocked=$(printf "%.f" "${domains_being_blocked_raw}") @@ -421,7 +421,7 @@ GetPiholeInformation() { ftl_dns_port=$(GetFTLData "dns-port") # ${ftl_dns_port} == 0 DNS server part of dnsmasq disabled, ${ftl_status} == "Not running" no ftlPID found - if [[ ${ftl_dns_port} == 0 ]] || [[ ${ftl_status} == "Not running" ]]; then + if [ "${ftl_dns_port}" = 0 ] || [ "${ftl_status}" = "Not running" ]; then pihole_status="DNS Offline" pihole_heatmap=${red_text} pihole_check_box=${check_box_bad} @@ -431,12 +431,12 @@ GetPiholeInformation() { full_status_=${full_status_dns_down} mega_status=${mega_status_dns_down} else - if [[ ${blocking_status} == "enabled" ]]; then + if [ "${blocking_status}" = "enabled" ]; then pihole_status="Active" pihole_heatmap=${green_text} pihole_check_box=${check_box_good} fi - if [[ ${blocking_status} == "disabled" ]]; then + if [ "${blocking_status}" = "disabled" ]; then pihole_status="Blocking disabled" pihole_heatmap=${red_text} pihole_check_box=${check_box_bad} @@ -446,7 +446,7 @@ GetPiholeInformation() { full_status_=${full_status_off} mega_status=${mega_status_off} fi - if [[ ${blocking_status} == "unknown" ]]; then + if [ "${blocking_status}" = "unknown" ]; then pihole_status="Unknown" pihole_heatmap=${yellow_text} pihole_check_box=${check_box_question} From 0b01f85f814ba27cdf1f091e981913508d45b21c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 20 Feb 2022 09:40:05 +0100 Subject: [PATCH 08/14] Remove unused PADD.PID file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/padd.sh b/padd.sh index 11338d01..efa14289 100755 --- a/padd.sh +++ b/padd.sh @@ -131,7 +131,7 @@ GetSummaryInformation() { clients=$(echo "${summary}" | grep "unique_clients" | grep -Eo "[0-9]+$") blocking_status=$(echo "${summary}" | grep "status" | grep -Eo "enabled|disabled|unknown" ) - + domains_being_blocked_raw=$(echo "${summary}" | grep "domains_being_blocked" | grep -Eo "[0-9]+$") domains_being_blocked=$(printf "%.f" "${domains_being_blocked_raw}") @@ -1032,10 +1032,7 @@ StartupRoutine(){ CheckConnectivity "$1" printf "%b" "Starting PADD...\n" - # Get PID of PADD - pid=$$ - printf "%b" " [■·········] 10%\\r" - echo ${pid} > ./PADD.pid + echo -ne " [■·········] 10%\\r" # Check for updates printf "%b" " [■■········] 20%\\r" @@ -1067,10 +1064,6 @@ StartupRoutine(){ CheckConnectivity "$1" echo "Starting PADD." - # Get PID of PADD - pid=$$ - echo "- Writing PID (${pid}) to file." - echo ${pid} > ./PADD.pid # Check for updates echo "- Checking for version file." @@ -1107,11 +1100,6 @@ StartupRoutine(){ printf "%b" "- Checking internet connection...\n" CheckConnectivity "$1" - # Get PID of PADD - pid=$$ - echo "- Writing PID (${pid}) to file..." - echo ${pid} > ./PADD.pid - # Check for updates echo "- Checking for PADD version file..." if [ -f "$piHoleVersion_file" ]; then From 756a32991fa08ea160b464ac18f6683a3549396f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Fri, 25 Mar 2022 19:11:03 +0100 Subject: [PATCH 09/14] Use /tmp instead of padd.sh dir to store temp files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/padd.sh b/padd.sh index efa14289..8a275a5f 100755 --- a/padd.sh +++ b/padd.sh @@ -11,10 +11,14 @@ LC_ALL=C LC_NUMERIC=C -# cd to the directory this script is stored in -cd "$(dirname "$0")" || { +# get the local temp directory +# credits to https://unix.stackexchange.com/a/174818 +tmpdir=$(dirname "$(mktemp -u)") + +# cd into the temp directory +cd "$tmpdir" || { EC=$? - echo >&2 "Could not chdir to the directory containing padd.sh (exit code $EC)" + echo >&2 "Could not chdir to the temp directory (exit code $EC)" exit $EC } From 9d67778e805a04f211c39d07d7c1063ed1523ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sat, 26 Mar 2022 15:00:43 +0100 Subject: [PATCH 10/14] Create subdir /tmp/PADD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/padd.sh b/padd.sh index 8a275a5f..ed477c8f 100755 --- a/padd.sh +++ b/padd.sh @@ -15,8 +15,9 @@ LC_NUMERIC=C # credits to https://unix.stackexchange.com/a/174818 tmpdir=$(dirname "$(mktemp -u)") -# cd into the temp directory -cd "$tmpdir" || { +mkdir -p "$tmpdir/PADD/" +# cd into the /temp/PADD/ directory +cd "$tmpdir/padd/" || { EC=$? echo >&2 "Could not chdir to the temp directory (exit code $EC)" exit $EC From 5f7a404677f578738a63d451693476fae74d19ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 27 Mar 2022 15:06:29 +0200 Subject: [PATCH 11/14] Create random padd.XXX temp dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/padd.sh b/padd.sh index ed477c8f..1ec7c006 100755 --- a/padd.sh +++ b/padd.sh @@ -11,13 +11,11 @@ LC_ALL=C LC_NUMERIC=C -# get the local temp directory -# credits to https://unix.stackexchange.com/a/174818 -tmpdir=$(dirname "$(mktemp -u)") +# creates a new local temp directory +tmpdir=$(mktemp -d --tmpdir padd.XXX) -mkdir -p "$tmpdir/PADD/" -# cd into the /temp/PADD/ directory -cd "$tmpdir/padd/" || { +# cd into the newly created /tmp/padd.XXX/ directory +cd "$tmpdir" || { EC=$? echo >&2 "Could not chdir to the temp directory (exit code $EC)" exit $EC From 74898cae748ec50314e6acc4903f589100e1111c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 27 Mar 2022 20:06:02 +0200 Subject: [PATCH 12/14] Use uid for temp dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/padd.sh b/padd.sh index 1ec7c006..96f2cfc6 100755 --- a/padd.sh +++ b/padd.sh @@ -11,11 +11,12 @@ LC_ALL=C LC_NUMERIC=C -# creates a new local temp directory -tmpdir=$(mktemp -d --tmpdir padd.XXX) +# creates a new local temp directory /tmp/padd_uid +tmpdir=$(dirname "$(mktemp -u)") +mkdir -p "$tmpdir/padd_$(id -u)/" -# cd into the newly created /tmp/padd.XXX/ directory -cd "$tmpdir" || { +# change into the newly created directory +pushd "$tmpdir/padd_$(id -u)/" > /dev/null || { EC=$? echo >&2 "Could not chdir to the temp directory (exit code $EC)" exit $EC @@ -1025,6 +1026,7 @@ CheckConnectivity() { OutputJSON() { GetSummaryInformation echo "{\"domains_being_blocked\":${domains_being_blocked_raw},\"dns_queries_today\":${dns_queries_today_raw},\"ads_blocked_today\":${ads_blocked_today_raw},\"ads_percentage_today\":${ads_percentage_today_raw},\"clients\": ${clients}}" + popd > /dev/null || exit } StartupRoutine(){ @@ -1207,6 +1209,7 @@ if [ $# = 0 ]; then # Run PADD clear NormalPADD + popd > /dev/null || exit fi for var in "$@"; do From b56060754893e393ba1f78d1ab0f765c6425097a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 22 May 2022 14:02:37 +0200 Subject: [PATCH 13/14] Use cd instead of pushd/popd because it's a bash built-in and no provided by /bin/sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/padd.sh b/padd.sh index 96f2cfc6..e7416360 100755 --- a/padd.sh +++ b/padd.sh @@ -16,7 +16,8 @@ tmpdir=$(dirname "$(mktemp -u)") mkdir -p "$tmpdir/padd_$(id -u)/" # change into the newly created directory -pushd "$tmpdir/padd_$(id -u)/" > /dev/null || { +oldpath=$(pwd) +cd "$tmpdir/padd_$(id -u)/" > /dev/null || { EC=$? echo >&2 "Could not chdir to the temp directory (exit code $EC)" exit $EC @@ -1026,7 +1027,7 @@ CheckConnectivity() { OutputJSON() { GetSummaryInformation echo "{\"domains_being_blocked\":${domains_being_blocked_raw},\"dns_queries_today\":${dns_queries_today_raw},\"ads_blocked_today\":${ads_blocked_today_raw},\"ads_percentage_today\":${ads_percentage_today_raw},\"clients\": ${clients}}" - popd > /dev/null || exit + cd "$oldpath" > /dev/null || exit } StartupRoutine(){ @@ -1037,7 +1038,7 @@ StartupRoutine(){ CheckConnectivity "$1" printf "%b" "Starting PADD...\n" - echo -ne " [■·········] 10%\\r" + printf "%b" " [■·········] 10%\\r" # Check for updates printf "%b" " [■■········] 20%\\r" @@ -1209,7 +1210,7 @@ if [ $# = 0 ]; then # Run PADD clear NormalPADD - popd > /dev/null || exit + cd "$oldpath" > /dev/null || exit fi for var in "$@"; do From 2fc7fc30f8cc298c95b7c9ee80b251e80907807c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Mon, 23 May 2022 09:58:58 +0200 Subject: [PATCH 14/14] Bump version to 3.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/padd.sh b/padd.sh index e7416360..7cdb6e92 100755 --- a/padd.sh +++ b/padd.sh @@ -26,7 +26,7 @@ cd "$tmpdir/padd_$(id -u)/" > /dev/null || { ############################################ VARIABLES ############################################# # VERSION -padd_version="v3.6.7" +padd_version="v3.7.0" # DATE today=$(date +%Y%m%d)