YouTube script seems to be working very well

Hi! You're a mind reader. I'll merge it in later, thanks. There's no sanity checking on any of the input as it's a proof of concept. It's apparently working well for everyone who tries it, so a safer/saner version will come.

Nice script! Could you please explain why it seems to be working?
If I look up hostnames similar to r1---sn-5hne6nsr.googlevideo.com I always get different IP addresses. If I execute your awk manually with these different IP addresses I also get always results back.

So what changes if we block one of them? There are, according to my log, hundreds of different IP addresses available - and we choose only one of them.

This doesn't block hosts, it forces them to an IP of our choosing. Also note there was some ambiguity between the various pihole restart* options that had me off on the wrong direction for reloading.
If you update the script now it should be good.

Great work by the way. Wouldn’t reload-lists be a better option than reload to avoid the cache getting dumped?

Thanks

Here is an Example for setting up ipv6 for the host file. (I run a dual stack -diverting ads 100% of the time will not work without ipv6 host additions.)

#!/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:
#     r6---sn-ni5f-tfbl.googlevideo.com

# as root: run this once then run "pihole restartdns"
# You can cron this for auto-updating of the host file.
# Mine fires every minute:
# * * * * * /home/grub/bin/youtube.update.sh 2>&1

forceIP="24.244.14.177"
#added for ipv6
#forceIP6="2001:4e8:0:b043::11"

# nothing below here should need changing

piLogs="/var/log/pihole.log"
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
    piLogs="$piLogs*" # preload with results from all logs
    echo "Setup complete! Execute 'pihole restartdns' as root."
    echo "cron the script to run every minute or so for updates."
fi

cp $ytHosts $workFile
zgrep -e "reply.*-.*\.googlevideo.*\..*\..*\..*" $piLogs \
    | awk -v fIP=$forceIP '{ print fIP, $6 }' >> $workFile
#this section added for ipv6
#zgrep -e "$forceIP" $workFile \
#    | awk -v fIP6=$forceIP6 '{ print fIP6, $2 }' >> $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

You must uncomment the ipv6 sections and add an appropriate ipv6 address based on the same NSlookup you did for the ipv4 nslookup.

also uncomment the zgrep section for ip6

edit
updated the ipv6 section.

No. See here

So far all is good, I have captured 198 domains.

Thank you. I’ve updated the script

Bit of a noob how do you check your logs ?

Ah, right, now I understand it.
Do you also know why it works?

Cheers

Thanks for your time and patience to come up with that simple but very effective method.
I am the sole developer of Diversion, a router ad-blocker for Asus routers running a customized Asuswrt-Merlin firmware.

I ported your script into Diversion, with fully automatic IPv4 and IPv6 forced IP setup (if IPv6 is enabled on the router). It features a reset and enable/disable function within the UI.
I am currently in beta 3 phase and plan to make this feature a permanent part of Diversion. That is, as long as Googs does not block that loophole you found.

Discussion is at SmallNetBuilder forums and the Diversion website is diversion.ch.

1 Like

With the recent changes coming for Pi-hole 5 will this script still work? I know there have been changes to the list files to a sql database. Would like to know if this script is not supported on Pi-hole 5 if it would get updated to support it.

I'm new to the Raspberry Pi (Pi 4 2gb) and the Pi-hole. I'm interested in this script and looking forward to Pi-hole 5 but since I think I've worked out the Google Fiber bugs with the Pi-hole I'm not sure about doing a rebuild if I'll not have to.

Yes it still works with v5.0

1 Like

integrate in 5.0 ?

Will the host names (example: r1---sn-5hne6nsr.googlevideo.com) show in the blacklist on the web console GUI?

Is there a reason to set the cron to every minute and not maybe every 5-10 minutes?

I'm new to this and trying to set this up. First all my YouTube videos appear to be using ipv6 in the query logs, will this only block ipv4 requests or will it also block ipv6 requests?

Is this a valid address to do a nslookup on r5---sn-vgqskn76.googlevideo.com or r6---sn-bvvbax4pcxg-naje.googlevideo.com? I get a error each time I do a nslookup.
r6---sn-bvvbax4pcxg-naje.googlevideo.com is not a legal IDN name (string contains forbidden two hyphens pattern), use +noidnin

use like this:

dig r5---sn-vgqskn76.googlevideo.com +noidnin +noidnout

or use any online nslookup:

https://ping.eu/nslookup/

1 Like

I'm getting this error running the script and confirmed none of the files it's supposed to create have been created.

./youtube.update.sh /bin/bash: crappy hack that seems to keep YouTube: No such file or directory

Same error with or without sudo.

if you copied and pasted the details you'll nee to update the " and # symbols in the script as its picking up some text in the comments section.

hope this helps