YouTube script seems to be working very well

Ok. implemented in my network with a shared list between 3 devices. Purged everything and now it's a matter of time ... I'll let you know in a couple of days how it looks on my side.

Be sure to schedule it to update. Mine runs every minute, it's just a blip of CPU.

It only took a few hours to see the difference.

I must say, I am pleased (and surprised) with the way this works.

Especially when (my) 3 Pi-hole instances gather all the data in one file (whomever catches one link that's not "recorded" yet, ads it to the centralized list), and all of them use that as the reference hosts.youtube file.

Very simple and logic :slight_smile:

Bravo and Thank you for sharing !

1 Like

Let me ask again: do we really have to execute pihole restartdns only once after the first script execution? I guess it's also necessary to regularly run pihole restartdns to re-load the changed file (/etc/hosts.youtube).

Something like that. Any change to an additional file need to signal ftl to re-read. The flat files are read and stored in memory at startup, there is no watch on the file to update the in-memory store.

That's really interesting, I thought updated hosts files were read from on the fly like a hosts file and standard resolver. It's working so well for us that I never bothered to check. But, yeah, added 1.2.3.4 foo.bar.com to the hosts.youtube and it didn't resolve until I restarted the DNS.

The script doesn't execute restartdns, even at first launch. In the latest tweak it tells the user to at first launch.

Guess the script could "pihole restartdns" on its own, but I really wanted to leave that up to the user.

edit: I should ask: would there be any problems with doing a "pihole restartdns" with every update if I cron it for */15 minutes? Maybe I'll just do that with another cron job at midnight or something.

No one is as surprised as I was when I tried it. But the fact that there was was a lookup and new IP coming in immediately before an in-video ad kept gnawing at me. That's why I tried it out. I left it running for a solid day+ with a few of us binging on YouTube before I cleaned up what I wrote and shared it.

Glad it works for you!

It would fully restart everything every 15 minutes. Cache would be dumped and you could race things. Better option would be to just reload the lists.

  restartdns          Full restart Pi-hole subsystems
                        Add 'reload' to update the lists and flush the cache without restarting the DNS server
                        Add 'reload-lists' to only update the lists WITHOUT flushing the cache or restarting the DNS server
2 Likes

Perfect, thank you! I added that with kudos for ya. :wink:

Some suggestions:

You can reload every minute if you want, but I wouldn't suggest it. Check to see if the workFile has any changes and only then call the reload.

Edit: Moved -o to correct place.

cp $ytHosts $workFile
zgrep -e "reply.*-.*\.googlevideo.*\..*\..*\..*" $piLogs \
	| awk -v fIP=$forceIP '{ print fIP, $6 }' >> $workFile	
sort -u $workFile -o $workFile
if ! cmp $workFile $ytHosts; then
    mv $workFile $ytHosts
	/usr/local/bin/pihole restartdns reload-lists
fi
3 Likes

Nicer check, thanks!

As the script only sets a IPv4, what if also IPv6 is active?

nslookup r3.sn-4g5ednsy.googlevideo.com

Server: 127.0.0.1
Address: 127.0.0.1#53

Non-authoritative answer:
Name: r3.sn-4g5ednsy.googlevideo.com
Address: 74.125.173.136
Name: r3.sn-4g5ednsy.googlevideo.com
Address: 2a00:1450:4001::8

Nothing happens beyond its intended use of adding customized IPv4 hosts. I'm current blocking outgoing IPv6 on the VLANs that are home to our players/embedded/mobile devices. It wasn't a concern when testing the idea out.

1 Like

I really like the script and wanted to share my modifications which reports the number of added domains and new total

if ! cmp $workFile $ytHosts; then
    LINES_OLD=$(wc -l <$ytHosts)
    LINES_NEW=$(wc -l <$workFile)  
    DIFF=$((LINES_NEW-$LINES_OLD))  
    mv $workFile $ytHosts
    /usr/local/bin/pihole restartdns reload-lists
    echo "Added "$DIFF" domains, new total "$LINES_NEW"."
fi

I also modified the cron to output to syslog

 */5 * * * * /home/nanopi/youtube 2>&1 | /usr/bin/logger -t pihole_youtube_blocker 

syslog

May  1 20:45:01 localhost CRON[30144]: (root) CMD (/home/nanopi/youtube 2>&1 | /usr/bin/logger -t pihole_youtube_blocker)
May  1 20:45:01 localhost pihole_youtube_blocker: /tmp/tmp.UcCGz2xV5T /etc/hosts.youtube differ: char 493, line 11
May  1 20:45:01 localhost pihole_youtube_blocker: Added 4 domains, new total 44.
May  1 20:50:01 localhost CRON[30437]: (root) CMD (/home/nanopi/youtube 2>&1 | /usr/bin/logger -t pihole_youtube_blocker)
May  1 20:50:01 localhost pihole_youtube_blocker: /tmp/tmp.FgKl1SibrR /etc/hosts.youtube differ: char 309, line 7
May  1 20:50:02 localhost pihole_youtube_blocker: Added 2 domains, new total 46.
May  1 20:55:01 localhost CRON[30644]: (root) CMD (/home/nanopi/youtube 2>&1 | /usr/bin/logger -t pihole_youtube_blocker)

2 Likes

Looks handy! If/when more people report good results for this little idea, I might take some time to do it properly and submit a diff.

Big thanks for your hard work as definitely seeing a difference as think i only saw about one ad when using Youtube on my Firecube for a few hours yesterday

1 Like

Would it make a difference which cache is used?

What is the privacy gain if you don't log the domains in Pi-hole but still have them in unbound? Appears to be no privacy gain, you just have to find the information in another place.

1 Like

Sorry if it's a noob question. Will this work with multiple ip's? When I do a log query it returns several, here it is:
grep googlevideo.com /var/log/pihole.log

May 2 18:52:09 dnsmasq[188378]: reply r5.sn-nx57ynls.googlevideo.com is 173.194.152.123
May 2 18:52:11 dnsmasq[188378]: reply r2.sn-nx5e6ne6.googlevideo.com is 173.194.152.104
May 2 18:52:34 dnsmasq[188378]: cached r3.sn-nx57ynls.googlevideo.com is 173.194.152.121

It actually has many dns requests, kept the duplicates, May 2 18:52:09 dnsmasq[188378]: reply r5.sn-nx57ynls.googlevideo.com is 173.194 - Pastebin.com

Pick any one of them.