YouTube script seems to be working very well


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

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: 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 or I get a error each time I do a nslookup. is not a legal IDN name (string contains forbidden two hyphens pattern), use +noidnin

use like this:

dig +noidnin +noidnout

or use any online 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.

./ /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

Ok, I'll take a look at it when I can remote in from a computer instead of my phone.

sudo -i 

enter password 

cd /usr/local/bin


paste the text from script and change IP

crtl x, yes to save

than: chmod a+x


pihole restartdns

thats it!

Thank you for your help. Looks like I'm blocking 144 YouTube URL's so far.

Glad it works for your users.

I haven’t had time over the weekend to merge in IPv6 or other functionality but hope to soon.

FWIW, I have a different method that seems to work better (only the very odd per-roll slips through), but it’s based on OpenBSD and pf. So it needs a different OS and it works down at the network layer.


It doesn't block, it redirects. :slight_smile:

Online lookup isn't optimal. Doing it locally will get you a geo-ip'd address closest to you (network-wise)

I was able to get the IP from using this command on my Raspberry Pi.

First of all - THANK you for finding this.

However, I'm still having some trouble.

What I did:

  • opened a YT video on my iPhone with the YouTube app which shows an ad in the beginning
  • grabbed the googlevideo string from PiHole log, ran nslookup on it and extracted the IPv4 and IPv6.
  • navigated to /usr/local/bin and created the with the following content:
#deleted the initial stuff to keep this post short
#added for ipv6
# nothing below here should need changing



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."

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
    rm $workFile

  • ran the script, restarted the PiHole DNS
  • added the script to crontab
* * * * * /usr/local/bin/ 2>&1

When I now open the YT video, the ads are skipped but the video doesn't load anymore. I checked the PiHole log and saw that was blocked (??) but even whitelisting that didn't fix it.

What did I do wrong? :frowning: Thank you!

Use the query lists tool to see where it was blocked.

All were blocked on the iPhone. The behavior is totally random:

  • either works fine and shows no ads
  • misses the ads completely, shows all ads and the video
  • already fails loading the ad (still shows Ad - 1 of 2) and gets stuck there

All with the the same two videos.

In the PiHole log, I can see it's blocking this for example: