Skip to content

Instantly share code, notes, and snippets.

@afgomez
Last active February 12, 2017 08:24
Show Gist options
  • Save afgomez/748896 to your computer and use it in GitHub Desktop.
Save afgomez/748896 to your computer and use it in GitHub Desktop.
Simple productivity booster
#!/bin/bash
# lets. A productivity booster
# Based on ideas from https://gist.github.com/605292
#
# Create a /etc/hosts.work with a list of sites you wanna block and execute
#
# $ sudo lets work
#
# When you finish your hard work, unblock the sites writing
#
# $ sudo lets play
#
blocked_file="/etc/hosts.work"
hosts="/etc/hosts"
function block_sites {
for host in $*; do
line="127.0.0.1 $host"
grep $host $hosts > /dev/null; append=$?
if [[ $append == 1 ]]; then
echo -e $line >> $hosts
fi
done
}
function unblock_sites {
for host in $*; do
sed -i '' "/127.0.0.1 $host/d" $hosts
done
}
if [ $UID -ne 0 ]; then
echo "Use me with sudo!" >&2
exit 2
fi
if [ -e $blocked_file ]; then
# Make a backup of the hosts file, just in case
cp $hosts $hosts.bak
blocked_hosts=()
while read line; do
# Ignore comment lines
if [[ $line == \#* ]]; then
continue
fi
blocked_hosts+=($line)
done < $blocked_file
case $1 in
"work" )
block_sites ${blocked_hosts[@]}
;;
"play" )
unblock_sites ${blocked_hosts[@]}
;;
"edit" )
$EDITOR $blocked_file
;;
*)
echo "Don't know what to do!" >&2
echo "Usage: lets [play|work|edit]" >&2
exit 1
;;
esac
dscacheutil -flushcache
echo "Lets $1!!"
else
echo "You must create $blocked_file first!"
exit 3
fi
@therobot
Copy link

mac os x only

@afgomez
Copy link
Author

afgomez commented Dec 23, 2010

Yep. Tendría que mirar como hacer el flush de dns en linux

Se aceptan sugerencias :)

@therobot
Copy link

Que yo sepa no hace falta. Y pensandolo un poco... yo en mac no he tenido que refrescar las dns cuando toco el /etc/hosts. ¿Has tenido algún problema?

@afgomez
Copy link
Author

afgomez commented Dec 23, 2010

La verdad es que no, pero no está de más ponerlo.

@therobot
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment