It might be nice to have a native hook into certain commands, e.g pihole -g
In this example, I am (probably not in the best way) removing ~25k gravity hosts that are not necessary due to my regex filters. I have yet to accommodate for wildcards (I use a conf file), but I expect this number to increase significantly.
My gravity list is updated a lot as I frequent run the manual updates on the dev channel, so running this script as a chron probably wouldn't be very effective, unless run many times a day.
It would be great if I could add this as an option to run automatically after each gravity update.
#!/usr/bin/env bash
# Set gravity.list location
file_out="/etc/pihole/gravity.list"
# Set regex.list location
file_regex="/etc/pihole/regex.list"
# Read regex config
echo "--> Reading regex criteria"
# Only read it if it exists and is not empty
if [ -s $file_regex ]; then
regexList=$(cat $file_regex | sort -u)
else
echo "--> Regex list is empty or does not exist."
exit
fi
# Status update
echo "--> $(wc -l <<< "$regexList") regexps found"
# Read the pihole gravity list
echo "--> Reading gravity.list"
# Only read it if it exists and is not empty
if [ -s $file_out ]; then
gravityList=$(cat $file_out | sort -u)
else
echo "--> gravity.list is empty or does not exist"
exit
fi
# Status update
echo "--> $(wc -l <<< "$gravityList") gravity.list entries"
# Create empty variable to store garbage
garbage_hosts=
echo "--> Identifying unnecessary domains"
# For each regex entry
# Add any regex matches to an array
for regex in $regexList; do
garbage_hosts+=$(grep -E $regex $file_out)
done
# Remove any duplicates from unnecessary hosts
garbage_hosts=$(sort -u <<< "$garbage_hosts")
# Status update
echo "--> $(wc -l <<< "$garbage_hosts") unnecessary hosts identified"
# Remove unnecessary entries
echo "--> Removing unnecessary domains"
cleaned_hosts=$(comm -23 <(echo "$gravityList") <(echo "$garbage_hosts"))
# Status update
echo "--> gravity.list: $(wc -l <<< "$cleaned_hosts")"
# Output file
echo "--> Outputting $file_out"
echo "$cleaned_hosts" | sudo tee $file_out > /dev/null
# Refresh Pihole
echo "--> Sending SIGHUP to Pihole"
sudo killall -SIGHUP pihole-FTL