Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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" )
vim $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

mac os x only

@afgomez
Owner

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

Se aceptan sugerencias :)

@therobot

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
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.