Add ability to run commands / scripts after certain actions

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

Since Pi-Hole is open source, you should be able to modify your local code to meet your needs.

Absolutely - But surely future updates will continually revert the changes?

@mmotti: Very interested in your script, duplicated it and ran it on my pihole. The result: gravity list was reduced from 623959 to 598710. This is a great result, I wonder what the result would be if this method would be applied to users, running this solution?

Would you be so kind to share your regex.list? As I mentioned in another topic, I found this list on reddit.

@DL6ER: I'm NOT a Linux expert, nor a regex expert, but this looks like something that should be part of pihole, further reducing the size of the gravity list...

15 posts were split to a new topic: Cleaning gravity.list based on what is in regex.list