forked from jfarmer/procrast
/
procrast
executable file
·85 lines (70 loc) · 1.19 KB
/
procrast
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
79
80
81
82
83
84
85
#!/bin/sh
LISTNAME=$(basename $0)
create_host_line() {
echo "$1 $2 # $LISTNAME"
}
print_usage() {
echo "Usage: [sudo] $LISTNAME [no|yes|add|remove|list|dns_reload]"
}
block_list() {
sed -e "s/^#\(.*# $LISTNAME\)/\1/g" /etc/hosts > /etc/hosts.new
mv /etc/hosts.new /etc/hosts
}
unblock_list() {
sed -e "s/\(^[^\#]*# $LISTNAME\)/#\1/g" /etc/hosts > /etc/hosts.new
mv /etc/hosts.new /etc/hosts
}
add_host() {
local HOST=$1
if [ "$#" = "1" ];then
local IP=127.0.0.1
else
local IP=$2
fi
echo $(create_host_line $IP $HOST) >> /etc/hosts
}
remove_host() {
local HOST=$1
sed "s/.*$HOST # $LISTNAME//g" /etc/hosts > /etc/hosts.new
mv /etc/hosts.new /etc/hosts
}
list() {
cat /etc/hosts | grep "# $LISTNAME" | cut -d" " -f2
}
dns_reload() {
killall mDNSResponder
}
# Raise to sudo privileges for options requiring that
if [[ $1 != 'list' ]] && [[ ! -z $1 ]]
then
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
sudo $0 $*
exit
fi
fi
COMMAND=$1
shift
case $COMMAND in
'no')
block_list
;;
'yes')
unblock_list
;;
'add')
add_host $*
;;
'remove')
remove_host $*
;;
'list')
list
;;
'dns_reload')
dns_reload
;;
*)
print_usage
exit 1
;;
esac