Skip to content

Instantly share code, notes, and snippets.

@silv3rr
Last active August 15, 2020 10:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silv3rr/baea033d43c461f13d1bb18c44fa90d6 to your computer and use it in GitHub Desktop.
Save silv3rr/baea033d43c461f13d1bb18c44fa90d6 to your computer and use it in GitHub Desktop.
trying to block yt ads using pihole, and failing ;)
#!/bin/sh
BL=blacklist-yt.txt
LOGDIR=/var/log
#REGEX="r\([0-9]\|[0-9][0-9]\)---sn-.*\.googlevideo\.com"
REGEX="r\([0-9]\|[0-9][0-9]\)\.sn-.*\.googlevideo\.com"
DEBUG=1
if [ ! -f $BL ]; then echo "$BL does not exist, exiting..."; exit 1; fi
{ ps -o comm= --pid $$ | grep bash; } > /dev/null 2>&1 && shopt -s expand_aliases
alias yt="grep '"$REGEX"' $BL | wc -l"
alias total="cat $BL | wc -l"
alias dom_debug='printf "[DEBUG] domains in $BL %6s: %d youtube (%d total)\n" $str $(yt) $(total)'
alias dom_count='printf "added blacklisted domains: %d youtube (added %d total)\n" $(( $yt_after - $yt_before )) $(( $total_after - $total_before ))'
[ $DEBUG -eq 1 ] && { str=before; dom_debug; }
yt_before=$(yt); total_before=$(total)
mv $BL $BL.bak || echo "unable to move $BL to $BL.bak"
{ { cat $LOGDIR/pihole.log $LOGDIR/pihole.*.1; zcat $LOGDIR/pihole*gz; } | \
grep $REGEX | cut -d" " -f6; cat ${BL}.bak; } | sort -t r -k 2 -n | uniq > $BL
[ $DEBUG -eq 1 ] && { str=after; dom_debug; }
yt_after=$(yt); total_after=$(total); echo $(dom_count) | sed 's/-\([0-9]\+\)/\1/g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment