2015-12-06 13:55:50 +00:00
|
|
|
|
#!/usr/bin/env bash
|
2016-01-30 20:12:40 +00:00
|
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
|
|
|
|
# (c) 2015, 2016 by Jacob Salmela
|
|
|
|
|
# Network-wide ad blocking via your Raspberry Pi
|
|
|
|
|
# http://pi-hole.net
|
|
|
|
|
# Calculates stats and displays to an LCD
|
2015-12-06 13:55:50 +00:00
|
|
|
|
#
|
|
|
|
|
# Pi-hole is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or
|
2016-01-18 22:24:02 +00:00
|
|
|
|
# (at your option) any later version.
|
2016-01-18 22:03:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Functions##############################################################################################################
|
|
|
|
|
piLog="/var/log/pihole.log"
|
|
|
|
|
gravity="/etc/pihole/gravity.list"
|
|
|
|
|
|
2016-11-02 00:01:48 +00:00
|
|
|
|
. /etc/pihole/setupVars.conf
|
|
|
|
|
|
2017-01-12 21:23:58 +00:00
|
|
|
|
# Borrowed/modified from https://gist.github.com/cjus/1047794
|
|
|
|
|
function GetJSONValue {
|
2017-01-12 21:51:10 +00:00
|
|
|
|
retVal=$(echo $1 | sed 's/\\\\\//\//g' | \
|
2017-01-12 21:23:58 +00:00
|
|
|
|
sed 's/[{}]//g' | \
|
|
|
|
|
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | \
|
|
|
|
|
sed 's/\"\:\"/\|/g' | \
|
|
|
|
|
sed 's/[\,]/ /g' | \
|
|
|
|
|
sed 's/\"//g' | \
|
2017-01-12 21:51:10 +00:00
|
|
|
|
grep -w $2)
|
2017-01-12 21:23:58 +00:00
|
|
|
|
echo ${retVal##*|}
|
2016-01-18 22:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 02:47:45 +00:00
|
|
|
|
outputJSON() {
|
2017-01-12 21:51:10 +00:00
|
|
|
|
json=$(curl -s -X GET http://127.0.0.1/admin/api.php?summaryRaw)
|
2017-01-12 21:23:58 +00:00
|
|
|
|
echo ${json}
|
2016-01-18 22:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 02:47:45 +00:00
|
|
|
|
normalChrono() {
|
2016-10-22 07:14:38 +00:00
|
|
|
|
for (( ; ; )); do
|
2016-01-18 22:03:53 +00:00
|
|
|
|
clear
|
|
|
|
|
# Displays a colorful Pi-hole logo
|
2016-05-07 18:44:18 +00:00
|
|
|
|
echo " [0;1;35;95m_[0;1;31;91m__[0m [0;1;33;93m_[0m [0;1;34;94m_[0m [0;1;36;96m_[0m"
|
|
|
|
|
echo "[0;1;31;91m|[0m [0;1;33;93m_[0m [0;1;32;92m(_[0;1;36;96m)_[0;1;34;94m__[0;1;35;95m|[0m [0;1;31;91m|_[0m [0;1;32;92m__[0;1;36;96m_|[0m [0;1;34;94m|[0;1;35;95m__[0;1;31;91m_[0m"
|
|
|
|
|
echo "[0;1;33;93m|[0m [0;1;32;92m_[0;1;36;96m/[0m [0;1;34;94m|_[0;1;35;95m__[0;1;31;91m|[0m [0;1;33;93m'[0m [0;1;32;92m\/[0m [0;1;36;96m_[0m [0;1;34;94m\[0m [0;1;35;95m/[0m [0;1;31;91m-[0;1;33;93m_)[0m"
|
|
|
|
|
echo "[0;1;32;92m|_[0;1;36;96m|[0m [0;1;34;94m|_[0;1;35;95m|[0m [0;1;33;93m|_[0;1;32;92m||[0;1;36;96m_\[0;1;34;94m__[0;1;35;95m_/[0;1;31;91m_\[0;1;33;93m__[0;1;32;92m_|[0m"
|
|
|
|
|
echo ""
|
2016-11-06 17:16:48 +00:00
|
|
|
|
echo " ${IPV4_ADDRESS}"
|
2016-01-18 22:03:53 +00:00
|
|
|
|
echo ""
|
|
|
|
|
uptime | cut -d' ' -f11-
|
2016-03-08 18:19:01 +00:00
|
|
|
|
#uptime -p #Doesn't work on all versions of uptime
|
|
|
|
|
uptime | awk -F'( |,|:)+' '{if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0,"days,",h+0,"hours,",m+0,"minutes."}'
|
2016-01-18 22:03:53 +00:00
|
|
|
|
echo "-------------------------------"
|
2017-01-16 21:39:58 +00:00
|
|
|
|
domain=$(curl -s -X GET http://127.0.0.1/admin/api.php?recentBlocked)
|
|
|
|
|
echo "Recently blocked:"
|
|
|
|
|
echo " $domain"
|
2017-01-16 23:51:13 +00:00
|
|
|
|
# Uncomment to continually read the log file and display the current domain being blocked
|
2016-01-18 22:03:53 +00:00
|
|
|
|
#tail -f /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ {if ($7 != "address" && $7 != "name" && $7 != "/etc/pihole/gravity.list") print $7; else;}'
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2017-01-12 22:07:07 +00:00
|
|
|
|
json=$(curl -s -X GET http://127.0.0.1/admin/api.php?summaryRaw)
|
2017-01-12 21:51:10 +00:00
|
|
|
|
|
|
|
|
|
domains=$(printf "%'.f" $(GetJSONValue ${json} "domains_being_blocked")) #add commas in
|
|
|
|
|
queries=$(printf "%'.f" $(GetJSONValue ${json} "dns_queries_today"))
|
|
|
|
|
blocked=$(printf "%'.f" $(GetJSONValue ${json} "ads_blocked_today"))
|
2017-01-13 11:59:49 +00:00
|
|
|
|
LC_NUMERIC=C percentage=$(printf "%0.2f\n" $(GetJSONValue ${json} "ads_percentage_today")) #2 decimal places
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2017-01-12 21:23:58 +00:00
|
|
|
|
echo "Blocking: ${domains}"
|
|
|
|
|
echo "Queries: ${queries}"
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2017-01-12 21:23:58 +00:00
|
|
|
|
echo "Pi-holed: ${blocked} (${percentage}%)"
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
sleep 5
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 02:47:45 +00:00
|
|
|
|
displayHelp() {
|
2016-10-20 03:46:37 +00:00
|
|
|
|
cat << EOM
|
|
|
|
|
::: Displays stats about your piHole!
|
|
|
|
|
:::
|
|
|
|
|
::: Usage: sudo pihole -c [optional:-j]
|
|
|
|
|
::: Note: If no option is passed, then stats are displayed on screen, updated every 5 seconds
|
|
|
|
|
:::
|
|
|
|
|
::: Options:
|
|
|
|
|
::: -j, --json output stats as JSON formatted string
|
|
|
|
|
::: -h, --help display this help text
|
|
|
|
|
EOM
|
2016-12-01 23:10:06 +00:00
|
|
|
|
exit 0
|
2016-01-18 22:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
|
|
|
|
normalChrono
|
|
|
|
|
fi
|
|
|
|
|
|
2016-10-22 07:14:38 +00:00
|
|
|
|
for var in "$@"; do
|
|
|
|
|
case "$var" in
|
|
|
|
|
"-j" | "--json" ) outputJSON;;
|
|
|
|
|
"-h" | "--help" ) displayHelp;;
|
|
|
|
|
* ) exit 1;;
|
|
|
|
|
esac
|
2016-01-18 22:15:39 +00:00
|
|
|
|
done
|