From fb72ac9904818e72ce486eede6c9b10ae89011ab Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 6 Dec 2016 08:18:49 +0100 Subject: [PATCH 1/3] Show only exact matches for pihole -q --- pihole | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pihole b/pihole index 8adb15ed..e1210dc8 100755 --- a/pihole +++ b/pihole @@ -71,10 +71,10 @@ queryFunc() { domain="${2}" lists=( /etc/pihole/list.* /etc/pihole/blacklist.txt) for list in ${lists[@]}; do - count=$(grep -c ${domain} $list) + count=$(grep -c -E "(^|\s)${domain}($|\s)" $list) echo "::: ${list} (${count} results)" if [[ ${count} > 0 ]]; then - grep ${domain} ${list} + grep -E "(^|\s)${domain}($|\s)" ${list} fi echo "" done From 294df8690cd9bf7ef4560d76e9fffb6578814c4a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 6 Dec 2016 09:55:17 +0100 Subject: [PATCH 2/3] Do only one grep on each of the lists and count the number of non-empty lines in the result. Improves speed by factor of 2x --- pihole | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pihole b/pihole index e1210dc8..a59bbc3c 100755 --- a/pihole +++ b/pihole @@ -67,14 +67,20 @@ setupLCDFunction() { exit 0 } +scanList(){ + grep -E "(^|\s)${domain}($|\s)" "$1" +} + queryFunc() { domain="${2}" lists=( /etc/pihole/list.* /etc/pihole/blacklist.txt) for list in ${lists[@]}; do - count=$(grep -c -E "(^|\s)${domain}($|\s)" $list) + result=$(scanList $list) + # Remove empty lines before couting number of results + count=$(sed '/^\s*$/d' <<< "$result" | wc -l) echo "::: ${list} (${count} results)" if [[ ${count} > 0 ]]; then - grep -E "(^|\s)${domain}($|\s)" ${list} + echo $result fi echo "" done From c0886cb5c699d30149d71a0e1d33355234cc0f3d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 6 Dec 2016 13:18:01 +0100 Subject: [PATCH 3/3] pihole -q is partial matching (as before), pihole -q -exact is exact matching (new behavior) --- pihole | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pihole b/pihole index a59bbc3c..40d45e6a 100755 --- a/pihole +++ b/pihole @@ -68,19 +68,27 @@ setupLCDFunction() { } scanList(){ - grep -E "(^|\s)${domain}($|\s)" "$1" + domain="${1}" + list="${2}" + method="${3}" + if [[ ${method} == "-exact" ]] ; then + grep -E "(^|\s)${domain}($|\s)" "${list}" + else + grep "${domain}" "${list}" + fi } queryFunc() { domain="${2}" + method="${3}" lists=( /etc/pihole/list.* /etc/pihole/blacklist.txt) for list in ${lists[@]}; do - result=$(scanList $list) + result=$(scanList ${domain} ${list} ${method}) # Remove empty lines before couting number of results count=$(sed '/^\s*$/d' <<< "$result" | wc -l) echo "::: ${list} (${count} results)" if [[ ${count} > 0 ]]; then - echo $result + echo "${result}" fi echo "" done @@ -224,6 +232,7 @@ helpFunc() { ::: -h, help Show this help dialog ::: -v, version Show current versions ::: -q, query Query the adlists for a specific domain +::: Use pihole -q domain -exact if you want to see exact matches only ::: -l, logging Enable or Disable logging (pass 'on' or 'off') ::: -a, admin Admin webpage options ::: uninstall Uninstall Pi-Hole from your system :(!