forked from iwangzz/hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlhosts
executable file
·164 lines (135 loc) · 3.35 KB
/
lhosts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
# Update your hosts file from:
# https://github.com/racaljk/hosts
#
myname=${0##*/}
HOSTS="/etc/hosts"
BACKUP_FILE="/etc/hosts.$myname.$(date +%Y%m%d%H%M%S).bak"
REMOTE_FILE="/tmp/hosts.rmt"
MAIN="https://raw.githubusercontent.com/racaljk/hosts/master/hosts"
MIRROR="https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts"
HOSTS_URL="$MAIN"
NET_TOOLS="curl"
QWGET=
QCURL=
RANGE=
BEGIN_MARK="#-- $myname BEGIN MARK --#"
END_MARK="#-- $myname END MARK --#"
usage()
{
cat <<EOL
提示:
1. 可以使用 crontab 定时执行脚本 (root 身份运行或 sudo 免密码);
2. 使用 $myname 下载的 hosts 会被加上范围标记,每次更新会保留范围外的
全部内容。首次使用 $myname 会自动保留原hosts内容,请放心使用。
3. 更新前,本地 hosts 备份至 $BACKUP_FILE 并每次保留时间戳,方便随时查找恢复
用法: $myname [选项]...
选项:
-m, --mirror 从镜像仓库获取 hosts (下载更快)
-q, --quiet 静默模式
-r, --range <range> 范围模式
-u, --url <url> 自定义 hosts 源地址
-w, --wget 使用 wget 下载
-h, -1, --help 显示帮助信息并退出
退出状态:
0 正常
1 命令行参数错误
2 文件下载失败
范围模式:
将本地 hosts 指定范围的内容,保存到下载的 hosts 内,例如:
$myname -mr "1,20" 更新时,本地 hosts 1~20 行保存到下载的 hosts 中
自定义源:
$myname -u $MIRROR
EOL
}
get_hosts()
{
local swp="/tmp/hosts.swp"
# If the quiet mode is turned off, print some messages.
if [ -z "$QCURL" ]; then
echo "正在更新 hosts..."
fi
if [ "$NET_TOOLS" = "wget" ]; then
# Use Wget to download.
"$NET_TOOLS" $QWGET "$HOSTS_URL" -O "$REMOTE_FILE"
else
# Use cURL to download.
"$NET_TOOLS" $QCURL "$HOSTS_URL" -#o "$REMOTE_FILE"
fi
if [ $? -ne 0 ]; then
echo "hosts 下载失败" >&2
exit 2
fi
# Add range mark
sed -e "1 i\\$BEGIN_MARK" -e "$ a\\$END_MARK" "$REMOTE_FILE" > "$swp"
mv -f "$swp" "$REMOTE_FILE"
}
backup_hosts()
{
sudo cp -f "$HOSTS" "$BACKUP_FILE"
}
update_hosts()
{
local swp="/tmp/hosts.swp"
if [ ! -z "$RANGE" ]; then
# Range mode on
sed -n "$RANGE"p "$HOSTS" > "$swp"
else
# Range mode off, auto set marker if no marker in the local hosts file.
if cat /etc/hosts | grep "$BEGIN_MARK"; then
echo 'Find MARK and handle the marker range only'
else
echo 'Not find MARK, Auto set marker and save hosts content'
echo $BEGIN_MARK | sudo tee -a /etc/hosts
echo $END_MARK | sudo tee -a /etc/hosts
echo 'Hosts update success!'
fi
# Range mode off, handle marker in the local hosts file.
if grep -q "$BEGIN_MARK" "$HOSTS"; then
sed "/$BEGIN_MARK/,/$END_MARK/d" "$HOSTS" >> "$swp"
fi
fi
cat "$REMOTE_FILE" >> "$swp"
sudo cp -f "$swp" "$HOSTS"
rm -f "$swp" "$REMOTE_FILE"
}
LONGOPTS="wget,mirror,quiet,url:,range:,help"
CMD=$(getopt -o wmqu:r:h1 --long $LONGOPTS -n "$myname" -- "$@") || exit 1
eval set -- "$CMD"
while true; do
case "$1" in
-w|--wget)
NET_TOOLS="wget"
shift
;;
-m|--mirror)
HOSTS_URL="$MIRROR"
shift
;;
-q|--quiet)
QWGET=-q
QCURL=-s
shift
;;
-u|--url)
HOSTS_URL="$2"
shift 2
;;
-r|--range)
RANGE="$2"
shift 2
;;
-h|-1|--help)
usage
exit 0
shift
;;
--)
shift
break
;;
esac
done
get_hosts
backup_hosts
update_hosts