目录

Github Hosts mac 自动更新

目录

最近 Github 经常连不上。写了个 Shell 工具,连不上的时候就更新一下Host。可以配合 Alfred 使用。

此工具已更新至Github。附有安装和卸载脚本。

使用的 host 源来自这里。其实用 SwitchHosts 更新也行,但我不喜欢小工具写成 Electron App……麻了,现在听个歌写个笔记 1个多 G 的内存就没了。

  • 下面这个文件保存取名为 hoststool
  • 赋予执行权限 chmod +x hoststool。如果为了执行起来方便,可以放进环境变量的目录(比如/usr/local/bin)
  • 然后就可以使用 hoststool -u 更新 host 了,运行结果会发系统通知。之前可以自己看看 hosts 内容有没有问题。这是有几率失败的,因为用的 gittee 的 host 源……这种时候重试一次就好了。
 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
#!/bin/bash
showHelp() {
	echo "-u [source_url]	update github hosts. You can specify a custom host source"
	echo "-b	backup /etc/hosts to /etc/hosts.backup"
	echo "-r	recover hosts.backup to hosts"
	echo "-f	list files in /etc which contains \"hosts\""
}


# ====== Main =====
if [ $# -eq 0 ]; then showHelp;exit 0;fi

# CONF
download_dir="$HOME/etc/hoststool"
github_hosts="https://gitee.com/yuchi-shentang/GithubHosts/raw/main/hosts.txt"
if [ ! -d "$download_dir" ]; then
  mkdir -p $download_dir
fi

case "$1" in
	-f)
		ls /etc | grep hosts;
		exit 0;;
	-b) 
		sudo cp /etc/hosts /etc/hosts.backup;
		exit 0;;
	-r) 
		sudo cp /etc/hosts.backup /etc/hosts;
		exit 0;;
	-u) 
		if [ $2 ]; then github_hosts=$2;fi
		curl -o ${download_dir}/hosts ${github_hosts};
		if [ $? -ne 0 ]; then
			echo "[ERROR] 获取远程 host 出错,请尝试更换 source 或检查 download_dir 读写权限"
			osascript -e 'display notification "获取远程 host 出错,请尝试更换 source" with title "hoststool"'
			exit 1
		fi
		
		# Validate host content length
		lines=$(awk '{print NR}' ${download_dir}/hosts | tail -n1)
		if [ $lines -lt 10 ]
		then
			echo '[ERRO] 远程 Github Hosts 无效(Gitee源不稳定),通常重试即可'
			osascript -e 'display notification "远程 Github Hosts 无效(Gitee源不稳定),通常重试即可" with title "hoststool"'
			rm ${download_dir}/hosts
			exit 1
		fi

		# Remove old content
		begin=$(sed -n  '/# ==== Github Start ====/=' /etc/hosts | awk 'NR==1{print}')
		end=$(sed -n  '/# ==== Github End ====/=' /etc/hosts | awk 'END{print}')
		echo "Removing old hosts. Start at line \"${begin}\", End at line \"${end}\""
		cat /etc/hosts | sed "${begin},${end}d" > ${download_dir}/hosts.tmp
		if [ $? -ne 0 ]; then
			## Trip Failed
			echo "[INFO] 当前 Host中 无旧的 Github Host 标记可清除"
		else
			## Trip Succeed, move result
			echo "[INFO] 清除旧的 Github Host 标记"
			sudo cp /etc/hosts /etc/hosts.backup && sudo cp ${download_dir}/hosts.tmp /etc/hosts;
		fi

		# Add new hosts
		sudo bash -c "echo '# ==== Github Start ====' >> /etc/hosts" # Add github host
		if [ $? -ne 0 ]; then
			echo "[ERROR] 无root权限,请尝试运行脚本手动输入密码"
			osascript -e 'display notification "无root权限,请尝试运行脚本手动输入密码" with title "hoststool"'
			rm ${download_dir}/hosts.tmp
			rm ${download_dir}/hosts
			exit 1;
		fi
		sudo bash -c "echo \"# Updated at $(date)\" >> /etc/hosts" # Add github host
		sudo bash -c "cat ${download_dir}/hosts >> /etc/hosts";
		sudo bash -c "echo '# ==== Github End ====' >> /etc/hosts"
		rm ${download_dir}/hosts.tmp
		rm ${download_dir}/hosts

		echo "[INFO] Github Hosts 块更新于 $(date)"
		osascript -e 'display notification "Github Hosts 已更新" with title "hoststool"'
		exit 0;;
	-h|--help)
		showHelp;
		exit 0;;
	*)
		echo "Unknown command";
		showHelp;
		exit 1;;
esac

定时任务

保存以下文件为 hoststool.plist

 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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>hoststool</string>

<key>ProgramArguments</key>
<array>
  <string>/usr/local/bin/hoststool</string>
  <string>-u</string>
</array>

<key>RunAtLoad</key>
<true/>

<key>StartInterval</key>
<integer>3600</integer>

<key>StandardOutPath</key>
<string>/Library/Logs/hoststool.log</string>

<key>StandardErrorPath</key>
<string>/Library/Logs/hoststool.log</string>

</dict>
</plist>

/usr/local/bin ,改成你存放脚本的路径。

时间间隔为 3600 秒,可以自己修改。

然后执行

1
2
3
$ sudo cp hoststool.plist /Library/LaunchDaemons
$ sudo chown root:admin /Library/LaunchDaemons/hoststool.plist
$ sudo launchctl load -w /Library/LaunchDaemons/hoststool.plist

执行完就会立刻运行一次脚本。由于定时任务是 root 用户不是个人用户,不会有通知,可以去 Console 看 log。