Starting from this post, but we’re going to keep doing it on a loop.
Note: I'm not seeing any obscure domain names like the original post mentioned, I just get the same set of a few IPs.
First backup your /etc/hosts.
Create a new file: /root/youtube_ad_blocker (sudo nano /root/youtube_ad_blocker
)
#!/usr/bin/env bash
# constants
hosts_file="/etc/hosts" # usually /etc/hosts
youtube_comment="# YouTube"
# create the YouTube section of the hosts file
if ! [[ -f "$hosts_file" ]] || ! grep -q "^${youtube_comment}$" $hosts_file; then
echo "$youtube_comment" >> $hosts_file
fi
# figure out which line we're starting at
line="$(grep -n '# YouTube' $hosts_file | cut -d ':' -f 1 | tr -d '\\n')"
# get all the ips currently listed in the hosts file
current_ips="$(tail -n +$(($line+1)) $hosts_file | sed -E 's/(.*)\\t.*$/\\1/')"
# get the latest ips
latest_ips="$(nslookup manifest.googlevideo.com | sed -n -E 's/^Address: ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})$/\\1/p')"
# merge the the current with the latest and keep only the unique entries
all_ips="$(echo -e "${current_ips}\\n${latest_ips}" | sort | uniq | tr '\\n' ' ')"
# rebuild the hosts file with the new ips list
# need to pull the top section into memory to avoid deleting the file contents)
top_of_file="$(head -n +$line $hosts_file)"
echo "$top_of_file" > $hosts_file
for ip in $all_ips; do
echo -e "${ip}\\tmanifest.googlevideo.com" >> $hosts_file
done
sudo chmod u+x /root/youtube_ad_blocker
Edit root
's crontab (sudo crontab -e
)
# gather new IPs every 2 minutes
*/2 * * * * /root/youtube_ad_blocker
# restart pihole DNS at 1am each day
0 1 * * * pihole restartdns
The result after a few days.
My /etc/hosts looks like this.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 orzo
# YouTube
142.250.64.110 manifest.googlevideo.com
142.250.64.78 manifest.googlevideo.com
142.250.65.238 manifest.googlevideo.com
142.250.72.110 manifest.googlevideo.com
142.250.80.46 manifest.googlevideo.com
142.250.80.78 manifest.googlevideo.com
142.250.81.238 manifest.googlevideo.com
142.251.32.110 manifest.googlevideo.com
142.251.35.174 manifest.googlevideo.com
This did not help with blocking any more ads. It seems like YouTube now has a backup in place: in my web inspector I'm seeing a lot of calls to youtube.com
with pagead
somewhere in the URL path, but since the domain name doesn't vary from where the actual website comes from, there's no way to block it with PiHole. I’m removing the script and aliases from my PiHole.
All YouTube aliases after several weeks.
142.250.176.206 manifest.googlevideo.com
142.250.64.110 manifest.googlevideo.com
142.250.64.78 manifest.googlevideo.com
142.250.65.174 manifest.googlevideo.com
142.250.65.206 manifest.googlevideo.com
142.250.65.238 manifest.googlevideo.com
142.250.72.110 manifest.googlevideo.com
142.250.80.110 manifest.googlevideo.com
142.250.80.14 manifest.googlevideo.com
142.250.80.46 manifest.googlevideo.com
142.250.80.78 manifest.googlevideo.com
142.250.81.238 manifest.googlevideo.com
142.251.32.110 manifest.googlevideo.com
142.251.35.174 manifest.googlevideo.com
142.251.40.110 manifest.googlevideo.com
142.251.40.142 manifest.googlevideo.com
142.251.40.174 manifest.googlevideo.com
142.251.40.206 manifest.googlevideo.com
142.251.40.238 manifest.googlevideo.com
142.251.41.14 manifest.googlevideo.com
172.217.165.142 manifest.googlevideo.com