-
Notifications
You must be signed in to change notification settings - Fork 6
/
youtube.update.sh
52 lines (39 loc) · 1.5 KB
/
youtube.update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# crappy hack that seems to keep YouTube ads to a minumum.
# over two hours of Peppa Pig and no ads. Taking one for the team...
# [email protected] v0.11
# Change forceIP to the real IP from an nslookup of a
# googlevideo hostname so you get something in your
# geographical region. You can find one in your
# Pi-hole's query logs.
# They will look something like this:
# r6---sn-ni5f-tfbl.googlevideo.com
# as root: run this once then run "pihole restartdns"
# You can cron this for auto-updating of the host file.
# Mine fires every minute:
# * * * * * /home/grub/bin/youtube.update.sh 2>&1
forceIP="123.456.789.999"
# nothing below here should need changing
piLogs="/var/log/pihole.log"
ytHosts="/etc/hosts.youtube"
workFile=$(mktemp)
dnsmasqFile="/etc/dnsmasq.d/99-youtube.grublets.conf"
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."
fi
cp $ytHosts $workFile
zgrep -E "reply.r[0-9-]-*sn-[0-9a-z]{8}.googlevideo*|reply.r[0-9-]-*sn-[0-9a-z]{12}-[0-9a-z]{4}.googlevideo*|reply.*-.*\.googlevideo.*\..*\..*\..*" $piLogs \
| awk -v fIP=$forceIP '{ print fIP, $6 }' >> $workFile
sort -u $workFile -o $workFile
if ! cmp $workFile $ytHosts; then
mv $workFile $ytHosts
chmod 644 $ytHosts
/usr/local/bin/pihole restartdns reload
else
rm $workFile
fi
exit