Skip to content

Instantly share code, notes, and snippets.

@arulrajnet
Created June 22, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arulrajnet/7ce4f42641511defb27bcaf04eeec835 to your computer and use it in GitHub Desktop.
Save arulrajnet/7ce4f42641511defb27bcaf04eeec835 to your computer and use it in GitHub Desktop.
Script for point all youtube hostnames to single IP to minimize the youtube ads.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment