From c3023fe68182c7c9adf2c39e86576cedec1bf942 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 30 May 2019 21:23:15 +0200 Subject: [PATCH 1/6] Add new "pihole arpflush" command to flush both the ARP cache as well as the network table in pihole-FTL.db Signed-off-by: DL6ER --- advanced/Scripts/piholeARPTable.sh | 65 ++++++++++++++++++++++++++++++ pihole | 10 ++++- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100755 advanced/Scripts/piholeARPTable.sh diff --git a/advanced/Scripts/piholeARPTable.sh b/advanced/Scripts/piholeARPTable.sh new file mode 100755 index 00000000..6af96d91 --- /dev/null +++ b/advanced/Scripts/piholeARPTable.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# shellcheck disable=SC1090 + +# Pi-hole: A black hole for Internet advertisements +# (c) 2019 Pi-hole, LLC (https://pi-hole.net) +# Network-wide ad blocking via your own hardware. +# +# ARP table interaction +# +# This file is copyright under the latest version of the EUPL. +# Please see LICENSE file for your rights under this license. + +coltable="/opt/pihole/COL_TABLE" +if [[ -f ${coltable} ]]; then + source ${coltable} +fi + +# Determine database location +# Obtain DBFILE=... setting from pihole-FTL.db +# Constructed to return nothing when +# a) the setting is not present in the config file, or +# b) the setting is commented out (e.g. "#DBFILE=...") +FTLconf="/etc/pihole/pihole-FTL.conf" +if [ -e "$FTLconf" ]; then + DBFILE="$(sed -n -e 's/^\s*DBFILE\s*=\s*//p' ${FTLconf})" +fi +# Test for empty string. Use standard path in this case. +if [ -z "$DBFILE" ]; then + DBFILE="/etc/pihole/pihole-FTL.db" +fi + + +flushARP(){ + local output + if [[ "$@" != *"quiet"* ]]; then + echo -ne " ${INFO} Flushing network table ..." + fi + + # Flush ARP cache to avoid re-adding of dead entries + if ! output=$(ip neigh flush all 2>&1); then + echo -e "${OVER} ${CROSS} Failed to clear ARP cache" + echo " Output: ${output}" + return 1 + fi + + if ! output=$(sqlite3 "${DBFILE}" "DELETE FROM network;" 2>&1); then + echo -e "${OVER} ${CROSS} Failed to truncate network table" + echo " Database location: ${DBFILE}" + echo " Output: ${output}" + return 1 + fi + + if [[ "$@" != *"quiet"* ]]; then + echo -e "${OVER} ${TICK} Flushed network table" + fi +} + +args=("$@") + +case "${args[0]}" in + "arpflush" ) flushARP;; +esac + +shift + diff --git a/pihole b/pihole index 71c286c6..d837f873 100755 --- a/pihole +++ b/pihole @@ -54,6 +54,11 @@ flushFunc() { exit 0 } +arpFunc() { + "${PI_HOLE_SCRIPT_DIR}"/piholeARPTable.sh "$@" + exit 0 +} + updatePiholeFunc() { shift "${PI_HOLE_SCRIPT_DIR}"/update.sh "$@" @@ -430,8 +435,8 @@ fi case "${1}" in "-w" | "whitelist" ) listFunc "$@";; "-b" | "blacklist" ) listFunc "$@";; - "--wild" | "wildcard" ) listFunc "$@";; - "--regex" | "regex" ) listFunc "$@";; + "--wild" | "wildcard" ) listFunc "$@";; + "--regex" | "regex" ) listFunc "$@";; "-d" | "debug" ) debugFunc "$@";; "-f" | "flush" ) flushFunc "$@";; "-up" | "updatePihole" ) updatePiholeFunc "$@";; @@ -452,5 +457,6 @@ case "${1}" in "checkout" ) piholeCheckoutFunc "$@";; "tricorder" ) tricorderFunc;; "updatechecker" ) updateCheckFunc "$@";; + "arpflush" ) arpFunc "$@";; * ) helpFunc;; esac From 285e6fe090069ffd38e6679b38256a9632e2f9e8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 30 May 2019 21:32:35 +0200 Subject: [PATCH 2/6] Address lint complaints Signed-off-by: DL6ER --- advanced/Scripts/piholeARPTable.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/advanced/Scripts/piholeARPTable.sh b/advanced/Scripts/piholeARPTable.sh index 6af96d91..dab730f4 100755 --- a/advanced/Scripts/piholeARPTable.sh +++ b/advanced/Scripts/piholeARPTable.sh @@ -32,7 +32,7 @@ fi flushARP(){ local output - if [[ "$@" != *"quiet"* ]]; then + if [[ "${args[1]}" != *"quiet"* ]]; then echo -ne " ${INFO} Flushing network table ..." fi @@ -43,6 +43,7 @@ flushARP(){ return 1 fi + # Truncate network table in pihole-FTL.db if ! output=$(sqlite3 "${DBFILE}" "DELETE FROM network;" 2>&1); then echo -e "${OVER} ${CROSS} Failed to truncate network table" echo " Database location: ${DBFILE}" @@ -50,7 +51,7 @@ flushARP(){ return 1 fi - if [[ "$@" != *"quiet"* ]]; then + if [[ "${args[1]}" != *"quiet"* ]]; then echo -e "${OVER} ${TICK} Flushed network table" fi } From 9ddce880920752cece01b48ad25caaeffe9275b8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 31 May 2019 08:42:22 +0200 Subject: [PATCH 3/6] Review comments Signed-off-by: DL6ER --- advanced/Scripts/piholeARPTable.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/advanced/Scripts/piholeARPTable.sh b/advanced/Scripts/piholeARPTable.sh index dab730f4..10bee0df 100755 --- a/advanced/Scripts/piholeARPTable.sh +++ b/advanced/Scripts/piholeARPTable.sh @@ -32,7 +32,7 @@ fi flushARP(){ local output - if [[ "${args[1]}" != *"quiet"* ]]; then + if [[ "${args[1]}" != "quiet" ]]; then echo -ne " ${INFO} Flushing network table ..." fi @@ -44,14 +44,14 @@ flushARP(){ fi # Truncate network table in pihole-FTL.db - if ! output=$(sqlite3 "${DBFILE}" "DELETE FROM network;" 2>&1); then + if ! output=$(sqlite3 "${DBFILE}" "DELETE FROM network" 2>&1); then echo -e "${OVER} ${CROSS} Failed to truncate network table" echo " Database location: ${DBFILE}" echo " Output: ${output}" return 1 fi - if [[ "${args[1]}" != *"quiet"* ]]; then + if [[ "${args[1]}" != "quiet" ]]; then echo -e "${OVER} ${TICK} Flushed network table" fi } @@ -61,6 +61,3 @@ args=("$@") case "${args[0]}" in "arpflush" ) flushARP;; esac - -shift - From 4947350ca5d946a66fdcb7952b4ef200d833e779 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 15 Jun 2019 09:06:10 +0200 Subject: [PATCH 4/6] Add arpflush to help and bash autocompletion Signed-off-by: DL6ER --- advanced/bash-completion/pihole | 2 +- pihole | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/advanced/bash-completion/pihole b/advanced/bash-completion/pihole index 7ba0dad8..cea36060 100644 --- a/advanced/bash-completion/pihole +++ b/advanced/bash-completion/pihole @@ -7,7 +7,7 @@ _pihole() { case "${prev}" in "pihole") - opts="admin blacklist checkout chronometer debug disable enable flush help logging query reconfigure regex restartdns status tail uninstall updateGravity updatePihole version wildcard whitelist" + opts="admin blacklist checkout chronometer debug disable enable flush help logging query reconfigure regex restartdns status tail uninstall updateGravity updatePihole version wildcard whitelist arpflush" COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) ;; "whitelist"|"blacklist"|"wildcard"|"regex") diff --git a/pihole b/pihole index d837f873..9fa65a8f 100755 --- a/pihole +++ b/pihole @@ -408,7 +408,8 @@ Options: Add '-h' for more info on disable usage restartdns Restart Pi-hole subsystems checkout Switch Pi-hole subsystems to a different Github branch - Add '-h' for more info on checkout usage"; + Add '-h' for more info on checkout usage + arpflush Flush information stored in Pi-hole's network tables"; exit 0 } From 435a5fb3adb8e38f38772f89260da9cf40441ba0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 15 Jun 2019 09:12:44 +0200 Subject: [PATCH 5/6] Add pihole arpflush to man page. Signed-off-by: DL6ER --- manpages/pihole.8 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/manpages/pihole.8 b/manpages/pihole.8 index bd7d0933..7a750f4c 100644 --- a/manpages/pihole.8 +++ b/manpages/pihole.8 @@ -351,6 +351,12 @@ Switching Pi-hole subsystem branches .br Switch to core development branch .br + +\fBpihole arpflush\fR +.br + Flush information stored in Pi-hole's network tables +.br + .SH "SEE ALSO" \fBlighttpd\fR(8), \fBpihole-FTL\fR(8) From 6996ffa451d4b4911d7394653c840cd738c2a6d6 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 15 Jun 2019 09:15:12 +0200 Subject: [PATCH 6/6] Also flush network_addresses table Signed-off-by: DL6ER --- advanced/Scripts/piholeARPTable.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/advanced/Scripts/piholeARPTable.sh b/advanced/Scripts/piholeARPTable.sh index 10bee0df..aa45f9ad 100755 --- a/advanced/Scripts/piholeARPTable.sh +++ b/advanced/Scripts/piholeARPTable.sh @@ -43,6 +43,16 @@ flushARP(){ return 1 fi + # Truncate network_addresses table in pihole-FTL.db + # This needs to be done before we can truncate the network table due to + # foreign key contraints + if ! output=$(sqlite3 "${DBFILE}" "DELETE FROM network_addresses" 2>&1); then + echo -e "${OVER} ${CROSS} Failed to truncate network_addresses table" + echo " Database location: ${DBFILE}" + echo " Output: ${output}" + return 1 + fi + # Truncate network table in pihole-FTL.db if ! output=$(sqlite3 "${DBFILE}" "DELETE FROM network" 2>&1); then echo -e "${OVER} ${CROSS} Failed to truncate network table"