Skip to content

Instantly share code, notes, and snippets.

@jult
Forked from JosepOli/whitelist.sh
Last active December 14, 2024 12:38
Show Gist options
  • Save jult/5d3b3b7428f8d22a7e5107b9d1abc0dc to your computer and use it in GitHub Desktop.
Save jult/5d3b3b7428f8d22a7e5107b9d1abc0dc to your computer and use it in GitHub Desktop.
Using URLs to whitelist on pihole server
#!/bin/bash
# BE SURE TO INSTALL dos2unix, wget, sed (apt install dos2unix wget sed -y) and change these to your preferences:
WHITELIST_URL='https://jult.net/whitelist.txt'
WHITELIST_URLX='https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/whitelist.txt'
CUSTOM_LIST='/root/io/whitey'
# Give your txt file a name, this will have the domains that this script will whitelist into pi-hole
WHITELIST_FILE_NAME='wlresult'
wget -qO - --limit-rate=1500k "$WHITELIST_URL" > /tmp/white1
sleep 1
wget -qO - --limit-rate=1500k "$WHITELIST_URLX" >> /tmp/white1
sleep 1
cat "$CUSTOM_LIST" >> /tmp/white1
sleep 1
# We're on a linux device here, so correct linebreaks
dos2unix -q -n /tmp/white1 /tmp/wlistnix
sleep 1
# gotta love sort (removing doubles)
sort -u /tmp/wlistnix > /tmp/wlistsort
# cleaning up faulty strings and invalid FQDNs
sed '/^[a-zA-Z0-9]/!d' < /tmp/wlistsort > /tmp/whiteclean
sed -r '/[.].*[a-zA-Z0-9][a-zA-Z0-9-]+([.][a-zA-Z]{2,15})?$/!d' < /tmp/whiteclean > "$WHITELIST_FILE_NAME"
# Check if the file exists
if [ ! -f "$WHITELIST_FILE_NAME" ]; then
echo "File $WHITELIST_FILE_NAME not found!"
exit 1
fi
# Loop through each line in your txt file and add it to the Pi-hole whitelist
while IFS= read -r url; do
# Trim the URL to remove possible white spaces and extract the domain
trimmed_url=$(echo "$url" | xargs)
# Use awk to extract the domain from a full URL
domain=$(echo "$trimmed_url" | awk -F/ '{print $3}')
# Check if the domain variable is not empty
if [ -n "$domain" ]; then
echo "Adding domain: $domain to the whitelist"
pihole -w "$domain"
fi
done < "$WHITELIST_FILE_NAME"
echo "All domains have been added to the whitelist, cleaning up the mess.."
# Let's make sure next run is a clean one
rm -rf /tmp/white*
rm -rf /tmp/wlist*
# Uncomment this if you want to clean the whitelist result, I leave it commented to check what has been whitelisted..
# rm -rf "$WHITELIST_FILE_NAME"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment