public
Last active

Simple productivity booster

  • Download Gist
lets.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#!/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

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

Se aceptan sugerencias :)

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?

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.