Created
March 8, 2011 01:43
-
-
Save lamberta/859683 to your computer and use it in GitHub Desktop.
Add and remove time-sink websites to /etc/hosts with a simple command.
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
#!/usr/bin/env bash | |
## Adds time consuming websites to /etc/hosts and redirects them to localhost. | |
## Make sure you backup /etc/hosts and set its location in the env var. | |
BLOCKED_SITES=("facebook.com" "login.facebook.com" "www.facebook.com" "twitter.com" "news.ycombinator.com" "www.nytimes.com") | |
BACKUP_HOSTS="$HOME/local/var/hosts.bak" | |
DEST_HOSTS="/etc/hosts" | |
function print_help { | |
echo "Usage: $(basename $0) [options]" | |
echo "Poor man's procrastination filter. Use $DEST_HOSTS to block website access." | |
echo " -h Show this usage guide." | |
echo " -B Block sites by adding them to $DEST_HOSTS." | |
echo " -U Unblock sites by restoring $DEST_HOSTS." | |
} | |
function block_sites { | |
if [ -f "$BACKUP_HOSTS" ]; then | |
echo -e "\n#blocked sites added by procras-filter" | sudo tee -a "$DEST_HOSTS" | |
for site in "${BLOCKED_SITES[@]}"; do | |
echo "127.0.0.1 $site" | sudo tee -a "$DEST_HOSTS" | |
done | |
exit 0 | |
else | |
echo "Error: Not writing to $DEST_HOSTS without backup file: $BACKUP_HOSTS" | |
exit 1 | |
fi | |
} | |
function unblock_sites { | |
if [ -f "$BACKUP_HOSTS" ]; then | |
echo " Restoring $DEST_HOSTS, requires root privileges..." | |
sudo cp "$BACKUP_HOSTS" "$DEST_HOSTS" | |
exit 0 | |
else | |
echo "Error: Unable to find backup hosts file: $BACKUP_HOSTS" | |
exit 1 | |
fi | |
} | |
#parse args | |
while getopts "BUh" option; do | |
case "$option" in | |
B) block_sites;; | |
U) unblock_sites;; | |
h) print_help; exit 0;; | |
\?) print_help; exit 0;; | |
esac | |
done | |
#no cammand-line args... | |
print_help | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment