Last active
February 12, 2017 08:24
-
-
Save afgomez/748896 to your computer and use it in GitHub Desktop.
Simple productivity booster
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
#!/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 | |
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.
Mis 0,002
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mac os x only