Skip to content

Instantly share code, notes, and snippets.

@arulrajnet
Last active May 12, 2021 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arulrajnet/9b78a93b72ac6fa473e18f8867e81b6d to your computer and use it in GitHub Desktop.
Save arulrajnet/9b78a93b72ac6fa473e18f8867e81b6d to your computer and use it in GitHub Desktop.
Script for point all youtube hostnames to single IP to minimize the youtube ads. Works with pihole

Fork of https://gitlab.com/grublets/youtube-updater-for-pi-hole

This fork has

  • Looking sqlite db for youtube domains instead of log file
  • Support for googlevideo.com and gvt1.com

Installation instructions are

YOUR_GEO_YT_IP="182.79.221.213"
curl -ksSL https://gist.githubusercontent.com/arulrajnet/9b78a93b72ac6fa473e18f8867e81b6d/raw/youtube.update.sh | sed "s/123.456.789.999/$YOUR_GEO_YT_IP/g" | sudo tee /usr/local/bin/youtube.update.sh
sudo chmod +x /usr/local/bin/youtube.update.sh

To run once

sudo /usr/local/bin/youtube.update.sh

To setup as cron to run every 5 min

(crontab -l 2>/dev/null; echo "*/5 * * * * sudo /usr/local/bin/youtube.update.sh 2>&1") | crontab -
#!/bin/bash
# crappy hack that seems to keep YouTube ads to a minumum.
# over two hours of Peppa Pig and no ads. Taking one for the team...
# grub@grub.net v0.11
# Change forceIP to the real IP from an nslookup of a
# googlevideo hostname so you get something in your
# geographical region. You can find one in your
# Pi-hole's query logs.
# They will look something like this:
# r3---sn-ci5gup-h55e.googlevideo.com
# r7---sn-ci5gup-h55l.gvt1.com
# as root: run this once then run "pihole restartdns"
# You can cron this for auto-updating of the host file.
# Mine fires every 5 minute:
#*/5 * * * * sudo /usr/local/bin/youtube.update.sh 2>&1
forceIP="123.456.789.999"
# nothing below here should need changing
piDB="/etc/pihole/pihole-FTL.db"
ytHosts="/etc/hosts.youtube"
workFile=$(mktemp)
dnsmasqFile="/etc/dnsmasq.d/99-youtube.grublets.conf"
if [ ! -f $dnsmasqFile ]; then
echo "addn-hosts=$ytHosts" > $dnsmasqFile
touch $ytHosts
echo "Setup complete! Execute 'pihole restartdns' as root."
echo "cron the script to run every 5 minute or so for updates."
fi
cp $ytHosts $workFile
sqlite3 $piDB "SELECT distinct domain FROM queries WHERE domain LIKE '%-%.googlevideo.com' OR domain LIKE '%-%.gvt1.com'" \
| awk -v fIP=$forceIP '{ print fIP, $1 }' >> $workFile
sort -u $workFile -o $workFile
if ! cmp $workFile $ytHosts; then
mv $workFile $ytHosts
chmod 644 $ytHosts
/usr/local/bin/pihole restartdns reload
else
rm $workFile
fi
exit
@DFlexy
Copy link

DFlexy commented Mar 15, 2021

Need help i have more 1 nslookup
how i use forceIP ?

@arulrajnet
Copy link
Author

@DFlexy

Need help i have more 1 nslookup
how i use forceIP ?

Define

At line 21

forceIP2="123.123.123.123"

At line 39. Change this to

| awk -v fIP=$forceIP -v fIP2=$forceIP2 '{ print fIP, $1  >> '\"$workFile\"'; print fIP2, $1  >> '\"$workFile\"'; }'

This should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment