Skip to content

Instantly share code, notes, and snippets.

@derixithy
Last active June 13, 2018 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derixithy/a766ba7e0901dc1b559a to your computer and use it in GitHub Desktop.
Save derixithy/a766ba7e0901dc1b559a to your computer and use it in GitHub Desktop.
Copy ad rules from mvps.org to /etc/hosts
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2016 Derixithy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
VERSION="2.0.1"
DOWNLOADURL="http://winhelp2002.mvps.org/hosts.txt"
RULEFILE="`mktemp /tmp/mvps-rules-XXX`"
NAMEFILE="/etc/hosts.name"
MERGEFILE="/etc/hosts"
trap cleanup EXIT
# Ceck dependancies
dep-check () {
if [[ -z "`which wget`" ]]; then
echo 'This script relies on wget to be installed'
exit 1
fi
}
# Proceed only if user is root
check-root () {
if [[ $EUID -ne 0 ]]; then
echo 'You need to be root to use this script'
exit 1
fi
}
# Cleanup when everything is done
cleanup () {
#echo 'Cleaning up'
rm "$RULEFILE"
}
# Download add rules from mvps.org
download () {
echo ' * downloading new rules'
#wget -nv $DOWNLOADURL -O $RULEFILE # maybe use as fallback
curl -# -o "$RULEFILE" "$DOWNLOADURL"
}
# Merge new add rules with the systems NAMEFILE
merge () {
check-root
{ cat $NAMEFILE; sed '23,26d' "$RULEFILE"; } > $MERGEFILE
}
# Setup system
setup () {
if [[ ! -f "$NAMEFILE" ]]; then
check-root
echo 'Running setup'
echo ' * Copy to system'
cp $0 /sbin
echo ' * copying hosts to hosts.name'
cp $MERGEFILE $NAMEFILE
if [[ $? -eq 1 ]]; then
echo 'Something went wrong.'
echo ' * Could not copy new hostfile'
exit 1
fi
download
merge
else
echo 'Nothing to setup.'
fi
}
# Check if setup has run
verify () {
if [[ ! -f "$NAMEFILE" ]]; then
echo "This system has not been setup"
read -p "Do you want to setup the system now? y/N" choice
case $choice in
y|Y|yes|Yes)
setup
;;
*)
exit
;;
esac
fi
}
# Show licence
licence () {
echo 'This program has been MIT licenced'
head -23 $0 | tail -n 20 -
echo 'Ad rules are downloaded from mvps.org, these come with there own licence'
echo '-----------------------------------------------------------'
if [[ -f "$NAMEFILE" ]]; then
head -35 "$MERGEFILE" | tail -n 21 -
else
echo ' * mvps licence has not been downloaded yet'
fi
}
# Show help menu
help () {
echo "Usage `basename $0` [option]"
echo 'Fetch ad rules from the internet and add them to the hostfile'
echo ''
echo ' update, -u --update Update the ad rules'
echo ' setup, -s --setup Install this script'
echo ' edit, -e --edit Edit NAMEFILE'
echo ' licence, -l --licence Show licence'
echo ' help, -h --help Show this help menu'
echo ''
echo 'This program has been create as-is'
echo 'Ad rules are downloaded from mvps.org, these come with there own licence'
echo "run `basename $0` to view full licence"
}
if [[ $# -eq 1 ]]; then
case "$1" in
update|-u|--update)
verify
download
merge
;;
setup|-s|--setup)
setup
;;
edit|-e|--edit)
edit
;;
licence|-l|--licence)
licence
;;
*)
help
;;
esac
else
help
fi
add wget fallback
enable dep-check
set permissions when installing to /sbin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment