forked from alswl/.oOo.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosts-on-off.zshrc
58 lines (50 loc) · 1.4 KB
/
hosts-on-off.zshrc
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
# hosts-on-off can managed your hosts files
# Feature: one click / groups / managed by zshrc.etc.d
#
# Required: hostess:
# brew install hostess
# sudo chown root /usr/local/Cellar/hostess/*/bin/hostess
# sudo chmod +s /usr/local/Cellar/hostess/*/bin/hostess
# Usage:
# put the following into ~/.zshrc.etc.d/hosts-sample.zshrc and `source ~/.zshrc.etc.d/hosts-sample.zshrc`
#
# test_com_hosts="
# 127.0.0.1 a.test.com
# 127.0.0.1 b.test.com
# "
# alias hosts-on-test="hosts-on test_com_hosts"
# alias hosts-off-test="hosts-off test_com_hosts"
function hosts-on {
hosts="$1"
if [ -z ${hosts} ]; then
print "error read \$hosts"
return
fi
hosts_content=$(printf '%s\n' "${(P)$(echo "$hosts")}")
while read -r line; do
ip=$(echo ${line} | awk '{print $1}')
domain=$(echo ${line} | awk '{print $2}')
if [ -z ${ip} ] || [ -z ${domain} ]; then
continue
fi
# chmod-root-s-hostess first
hostess add ${domain} ${ip}
done <<< "${hosts_content}"
}
function hosts-off {
hosts="$1"
if [ -z ${hosts} ]; then
print "error read \$hosts"
return
fi
hosts_content=$(printf '%s\n' "${(P)$(echo "$hosts")}")
while read -r line; do
ip=$(echo ${line} | awk '{print $1}')
domain=$(echo ${line} | awk '{print $2}')
if [ -z ${ip} ] || [ -z ${domain} ]; then
continue
fi
# chmod-root-s-hostess first
hostess rm ${domain};
done <<< "${hosts_content}"
}