Last active
May 9, 2016 20:14
-
-
Save mbround18/bc8083443ba473fd676faa59d0701775 to your computer and use it in GitHub Desktop.
ad_blocker_bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# This will download files or set the hosts file to defaults. | |
# USAGE: | |
# DESCRIPTION OF ENV VARS HERE | |
############################################################################### | |
set -e # exit on command errors (so you MUST handle exit codes properly!) | |
set -o pipefail # capture fail exit codes in piped commands | |
#set -x # execution tracing debug messages | |
# get switches | |
for i in "$@" | |
do | |
case $i in | |
--enable|-e) | |
ENABLED=true | |
shift # past argument with no value | |
;; | |
--disable|-d) | |
DISABLED=true | |
shift # past argument with no value | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
done | |
# allow hosts to be downloaded | |
if [ "$ENABLED" = true ]; then | |
# Hosts array this needs to be in text format. open any link for an example of hosts files online. that need to match that format | |
declare -a arr=( | |
"http://hosts-file.net/ad_servers.txt" | |
"http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext" | |
"http://winhelp2002.mvps.org/hosts.txt" | |
"https://adaway.org/hosts.txt" | |
) | |
# creating necessary files | |
touch /tmp/test_merge_adblock.txt | |
echo -e "# this file was generated at $(date) using the following host files:" > /tmp/hosts_complete | |
# now loop through the above array | |
for i in "${arr[@]}" | |
do | |
echo -e "downloading source host: $i" | |
TEMP_TXT_NAME=$(printf "$i" | sha256sum | sed 's/\ //g' | sed 's/\-//g') # this creates a unique name for the file so it is almost impossible to run into conflicts. | |
(curl "$i" > /tmp/"$TEMP_TXT_NAME"_adblock.txt) >/dev/null 2>&1 # downloading the sources from above with unique name | |
(sed -i 's/\r//' /tmp/"$TEMP_TXT_NAME"_adblock.txt) # converting proper line endings | |
cat /tmp/"$TEMP_TXT_NAME"_adblock.txt >> /tmp/test_merge_adblock.txt # merging into a common file | |
echo -e "# $i" >> /tmp/hosts_complete # adding source host url to completed host file for clerical purposes | |
done | |
echo -e "all hosts downloaded" | |
# removing comments from the file using sed. This is due to many hosts files have many comments in them. | |
(sed '/^#/ d' /tmp/test_merge_adblock.txt > /tmp/test_tmp_merge_adblock.txt) >/dev/null 2>&1 | |
mv -f /tmp/test_tmp_merge_adblock.txt /tmp/test_merge_adblock.txt | |
# now we remove duplicate lines this includes empty lines | |
awk '!seen[$0]++' /tmp/test_merge_adblock.txt >> /tmp/hosts_complete | |
# since the file is done, now we can move it to its proper place | |
sudo mv /tmp/hosts_complete /etc/hosts | |
# Setting the file to its original ownership in case this script was not run with sudo. | |
sudo chown root:root /etc/hosts | |
# clean up | |
rm -rf /tmp/*_adblock.txt | |
rm -rf /tmp/hosts_complete | |
echo -e "all hosts merged" | |
elif [ "$DISABLED" = true ]; then # reset the file to stock. | |
sudo rm -rf /etc/hosts | |
echo -e "resetting hosts file to stock" | |
echo -e "# | |
# /etc/hosts: static lookup table for host names | |
# | |
#<ip-address> <hostname.domain.org> <hostname> | |
127.0.0.1 localhost.localdomain localhost | |
::1 localhost.localdomain localhost | |
# End of file" > /tmp/hosts_complete | |
sudo mv /tmp/hosts_complete /etc/hosts | |
sudo chown root:root /etc/hosts | |
rm -rf /tmp/hosts_complete | |
echo -e "reset complete" | |
fi | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment