Skip to content

Instantly share code, notes, and snippets.

@knuxify
Last active December 4, 2019 18:13
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 knuxify/01488352382f560677dd21babcd8ea6b to your computer and use it in GitHub Desktop.
Save knuxify/01488352382f560677dd21babcd8ea6b to your computer and use it in GitHub Desktop.
Small domain blocker written in bash. Blocks domains in the /etc/hosts file.
#!/usr/bin/bash
# Domain blocker by knuxify
# This tool is based off the domain-blocker module, but adds a switch handler and additional output.
## User modifiable values:
unset domains
domains="youtube.com twitter.com discordapp.com nulled.red" # domains to block, separated by spaces
start() {
[[ ! -w /etc/hosts ]] && echo -e "\e[31m/etc/hosts is not writeable! Please retry as sudo or chown the hosts file so that you can write to it.\e[0m" && exit 1
sed -i -n '/#START\-DOMAIN\-BLOCK/,/#END\-DOMAIN\-BLOCK/!p' /etc/hosts
echo "#START-DOMAIN-BLOCK" >> /etc/hosts
for domain in $domains; do
echo -e "\e[32mBlocking $domain...\e[0m"
echo "127.0.0.1 $domain www.$domain" >> /etc/hosts
done
echo "#END-DOMAIN-BLOCK" >> /etc/hosts
echo -e "\e[32mBlocking successful!\e[0m"
}
end() {
[[ ! -w /etc/hosts ]] && echo -e "\e[31m/etc/hosts is not writeable! Please retry as sudo or chown the hosts file so that you can write to it.\e[0m" && exit 1
sed -i -n '/#START\-DOMAIN\-BLOCK/,/#END\-DOMAIN\-BLOCK/!p' /etc/hosts
echo -e "\e[32mUnblocking successful!\e[0m"
}
[[ -z "$*" ]] && echo -e "\e[31mNo action specified!\e[0m Available commands: start, end" && exit 1
case $* in
start) start;;
end) end;;
*) echo -e "\e[31mInvalid action specified!\e[0m Available commands: start, end"; exit 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment