How do I block ads on YouTube?

@DL6ER
Yes, I've tried it. The run command did not work proberly.

I was able to create the .sh install script, but the run command failed. I'm using Mac OS Terminal and connect to the Pi via ssh. This is, what it says:

"root@raspberrypi:~# chmod +x /etc/pihole/youtube-adblock-install.sh
root@raspberrypi:~# run /etc/pihole/youtube-adblock-install.sh
-bash: run: Kommando nicht gefunden."

The third line means "-bash: run: Did not find command."

Oh, that's not how you run a shell script. Just do:

./etc/pihole/youtube-adblock-install.sh

Well, but now it says:

root@raspberrypi:~# ./etc/pihole/youtube-adblock-install.sh
-bash: ./etc/pihole/youtube-adblock-install.sh: Datei oder Verzeichnis nicht gefunden

which means "file or directory not found".

Sorry for me being such a noob :smiley:

Edit: Haha okay, just need to leave out the first dot. Then it works just fine.

@Joshua_joezoef_B_ZzO

I followed your instruction and the pi hole seems to block almost any youtube ads now.

Only a few ads are getting through.

I had two error messages during the installation:

The first one is no error, just a note you can safely ignore.

About the second one ... what are the last lines of /etc/pihole/adlists.list ?

This is the content of the list:

GNU nano 2.7.4 Datei: /etc/pihole/adlists.list

##Zeustracker
https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist

##Disconnect.me Tracking
https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt

##Disconnect.me Ads
https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt

##Hosts-file.net
https://hosts-file.net/ad_servers.txt

www.youtube.com
#https://raw.githubusercontent.com/CamelCase11/UnifiedHosts/master/hosts.all
http://localhost/youtube-ads-list.txt

@Joshua_joezoef_B_ZzO

Awesome script, when I run it I get this error on the youtube-ads.sh

Blockquote
syntax error at -e line 1, at EOF
Missing right curly or square bracket at -e line 1, at end of line
Execution of -e aborted due to compilation errors.

you need to remove the line starting with perl -i -ne 'print if
in youtube-ads.sh

and replace it with

perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube-ads-list.txt

2 Likes

@Schrauber47 put a # in front of :this: entry and re-run pihole -g.

Hi and thanks for this script.
Right now I have about 150 domains in my list.

@itsme awesome, thank you for your help!

Is there a mistake in /etc/pihole/youtube-ads.sh ?
Line 10:
grep r*.googlevideo.com /var/log/pihole.log | awk '{print }'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt
Isn't anything missing after the awk'{print }'? Because it pastes the whole log there and not just the hostnames.
My idea was to just add the "$6" so it takes the hosts:
grep r*.googlevideo.com /var/log/pihole.log | awk '{print $6}'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt

Thank you for the great work and the idea!

Best Regards

Since I ruined the script, Pi-hole is keeping resolving ā€œdnsdumpster.com11ā€ is this expected?

I donā€™t know why, itā€™s seems to be coming from the Raspberry pi himself.

I don't understand why a such trafic and is that safe?

This is the original line from the script, but since the $ aren't escaped, the echo command tries to evaluate them. In short, the script line should be

echo "perl -i -ne 'print if ! \$x{\$_}++' /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh

Likewise the reason for the missing $6 that Cheesman97 mentioned:

echo "grep r*.googlevideo.com /var/log/pihole.log | awk '{print \$6}'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh

The escapes get swallowed by the echo otherwise...

I changed the script form @Joshua_joezoef_B_ZzO a bit (/etc/pihole/youtube-ads.s):

echo "" > /etc/dnsdumpster/youtube-domains.txt
echo "" > /etc/dnsdumpster/youtube-filtered.txt
echo "" > /etc/dnsdumpster/youtube-ads.txt
python /etc/dnsdumpster/dnsdumpster/ADS_youtube.py > /etc/dnsdumpster/youtube-domains.txt
grep ^r /etc/dnsdumpster/youtube-domains.txt >> /etc/dnsdumpster/youtube-filtered.txt
sed 's/\s.*$//' /etc/dnsdumpster/youtube-filtered.txt >> /etc/dnsdumpster/youtube-ads.txt
cat /etc/dnsdumpster/youtube-ads.txt > /var/www/html/youtube-ads-list.txt
#greps the log for youtube ads and appends to /var/www/html/youtube-ads-list.txt
grep r*.googlevideo.com /var/log/pihole.log /var/log/pihole.log.1 | awk '{print $6}'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt
#removes duplicate lines from /var/www/html/youtube-ads-list.txt
perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube-ads-list.txt
#updates pihole blacklist/whitelist
pihole -g

@idgers : On my rasbian it works without any escaping

Also make sure that the cronjob has a PATH variable:

*/15 * * * * PATH="$PATH:/usr/local/bin:/usr/bin" /etc/pihole/youtube-ads.sh >/dev/null

Something I found out:
It works on PC, but the YouTube-App on my iPhone doesn't work anymore (doesn't load any video).
I think the App can't handle not avaiable ads.

Huh, I'm on raspbian also. Maybe you used single quotes? Or maybe a paste messed something up when I went though it the other day...that's probably the most likely reason.

I found the same problem, though, where it doesn't work for mobile devices...ended up disabling it all since I already have a browser extension that handles it on PC. Wish I had more time to dig into it.

My list ends up being empty after running the script of @Joshua_joezoef_B_ZzO.

I do get the following error:
Traceback (most recent call last):
File "/etc/dnsdumpster/dnsdumpster/ADS_youtube.py", line 35, in
print("\n\n\nRetrieved XLS hosts? {} (accessible in 'xls_data')".format(xls_retrieved))
NameError: name 'xls_retrieved' is not defined

I made the adjustments @idgers suggested:
changed the following lines:
echo "perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh
echo "grep r*.googlevideo.com /var/log/pihole.log | awk '{print $6}'| grep -v '^googlevideo.com|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh

seems that dnsdumpster is failing (server code 500) when googlevideo.com is queried.
though it does appear to be working for others.

so if you folks see empty files, this is why :slight_smile:

First I will say thanks for the big work you all do

Are there any fix for line35 error

so, i remember fixing something, but unsure now what :confused:
but you are welcome to my copy of dnsdumpster folder ... getting an error from dnsdumpster site (but it happens if you go to dnsdumpster and query googlevideo.com regardless)

anyway, if you want - here is my dnsdumpster folder - http://buffalo.biodome.org/m/dnsdumpster.tgz