YouTube script seems to be working very well

According to dnsmasq documentation, only using the entire expression /#/ as a domain is treated to match any domain:

-A, --address=/<domain>[/<domain>...]/[<ipaddr>]
(..) /#/ matches any domain. Thus --address=/#/ will always return for any query not answered from /etc/hosts or DHCP and not sent to an upstream nameserver by a more specific --server directive. (..)

Accordingly, a quick test adding your line to my configuration resulted in failure:
pi@pi-hole-v5:~ $ nslookup

** server can't find NXDOMAIN

As Pi-hole embeds a tailored version of dnsmasq, you cannot simply switch to another dnsmasq fork.

However, if you are able to determine a sufficiently selective pattern for subdomain names to block, you could add them as a Pi-hole regex filter.

Indeed, working very well. I have it up and running, on a Win10 machine it removes all ads after skipping an initial first ad. Very sneaky of YouTube to have a discreet "Can I have an ad, please" request which returns the ad on a unique IP. Good job, thank you!!

1 Like

There are regex-capable forks of dnsmasq, I've only just started playing with them on Friday. Not sure how useful it would be as it would require porting to FTL from what I can tell.
Much easier to have the script scheduled so every day (week, whatever) it does outside DNS lookups on the hostnames already in the file, then repopulate the file with a new randomly picked IP from the new lookups.

Not necessary:

Ohh, what a bummer :confused: thx for clarifying.

But regarding regex blocking. Is it possible to setup custom redirections within pihole, too? I was under the impression the target result for all blocklists was set via a single parameter.

Blocking them doesn't work, they need to be redirected to an IP of our choosing.

Ah, I see - this solution relies on answering arbitrary youtube and googlevideos subdomains with a given custom IP, rather than just blocking them.

While this is certainly not Pi-hole's default behaviour, I can't answer if that's possible with pihole-FTL.

My son the Roblox/Terraria gamer says it's working. 91 on the list now..

May  4 10:41:02 raspberrypi pihole-youtube: /tmp/tmp.ujySzNA9IO /etc/ differ: byte 3117, line 66
May  4 10:41:02 raspberrypi pihole-youtube: Added 1 domains, new total 91.
1 Like

The idea is great, so is the script. I was looking at the script and asked myself if it is really necessary to parse the log file (thus eliminating unnecessary system load), we do have this information in the database (/etc/pihole/pihole-FTL.db).

To get the information, already in the database (first run):

sqlite3 "/etc/pihole/pihole-FTL.db" "SELECT "domain" FROM "queries" WHERE domain LIKE '';" | sed 's/---/./g' > /home/pi/result.txt

assuming a five minute interval (cron) to pick up new entries:

start="$(date  "+%R" -d "5 min ago")"
begintm=$(TZ=CET date --date="$start" +"%s")
sqlite3 "/etc/pihole/pihole-FTL.db" "SELECT "domain" FROM "queries" WHERE domain LIKE '' AND timestamp > $begintm;" | sed 's/---/./g' > /home/pi/result.txt

This may return an empty result (no matches in the last 5 minutes), so further processing may be skipped, if the query result is empty.
It's possible the LIKE string in the sqlite3 query may need to be improved

Be aware the response isn't immediate, by default the sqlite3 database (/etc/pihole/pihole-FTL.db) is written every minute, this can be modified by adding a setting (example DBINTERVAL=2) in /etc/pihole/pihole-FTL.conf


Interesting idea. Had me wondering how much time was used taking that route through the sqlite engine.
Not real profiling, but using time on the sqlite query on my VM:

real 0m0.005s
user 0m0.000s
sys 0m0.000s

Bash script:

real 0m0.001s
user 0m0.000s
sys 0m0.000s

Can i just double check i'm doing it right as bit of a noob

Logged into Pi via SSH
opened "cd /usr/local/bin"
then opened nano text editor "sudo nano"
And pasted text from 1 to 49 with updated forceIP with ip address from nslookup then saved plus did pihole restartdns

Seems to be working

Did you also set up the script to run every minute using the 'crontab'?
sudo crontab -e to edit the file. Add this line
* * * * * /usr/local/bin/
to the bottom of the file. Exit and save. It will now run every minute.
Check the syslog for cron output to verify sudo cat /var/log/syslog

1 Like

Big thanks as forget that bit

Think everything looks right

Hi all,

Please update the script and keep on testing, there's been a couple of bugs fixed (including a dumb one I added in the previous edit) and a potential permissions issue.

Gitlab link

Thanks @ jpgpi250.

1 Like

Crank'in away here, may05 version... The new version found 9 domains

May  5 15:18:01 raspberrypi pihole-youtube: /tmp/tmp.whbIsMrvrE /etc/ differ: byte 1030, line 22
May  5 15:18:02 raspberrypi pihole-youtube: Added 1 domains, new total 110.
May  5 15:47:01 raspberrypi pihole-youtube: /tmp/tmp.SEKBad40SQ /etc/ differ: byte 4668, line 99
May  5 15:47:01 raspberrypi pihole-youtube: Added 1 domains, new total 111.
May  5 15:49:01 raspberrypi pihole-youtube: /tmp/tmp.mpF6FgJHky /etc/ differ: byte 4997, line 106
May  5 15:49:02 raspberrypi pihole-youtube: Added 1 domains, new total 112.
May  5 15:57:01 raspberrypi pihole-youtube: /tmp/tmp.Ahrg3fpIMy /etc/ differ: byte 13, line 1
May  5 15:57:01 raspberrypi pihole-youtube: Added 9 domains, new total 121.
May  5 15:59:01 raspberrypi pihole-youtube: /tmp/tmp.24K5WvtZAV /etc/ differ: byte 175, line 4
May  5 15:59:01 raspberrypi pihole-youtube: Added 1 domains, new total 122.

I added a minor contribution, allowing the IP-address to be passed in as a parameter. I hope it meets with approval.

1 Like

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 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?