y
在Linux下载github的源码或者其他,下载速度缓慢,这个时候可以修改hosts文件来加速,但是加速地址并不固定,故写一脚脚本来测试速度并且设置这个加速地址。如果设置失败就回滚。
github加速
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
| #!/bin/bash
rollback_hosts() { echo "发生错误,正在恢复原始 hosts 文件..." mv /etc/hosts.backup /etc/hosts echo "hosts 文件已恢复。" }
if ! command -v nslookup &> /dev/null; then echo "nslookup 命令未找到,正在安装..." yum -y install bind-utils || { echo "安装 bind-utils 失败,脚本退出。"; exit 1; } fi
if ! command -v nscd &> /dev/null; then echo "nscd 命令未找到,正在安装..." yum install -y nscd || { echo "安装 nscd 失败,脚本退出。"; exit 1; } fi
fastly_ip=$(nslookup github.global.ssl.fastly.net | grep 'Address' | tail -n 1 | awk '{ print $2 }') github_ip=$(nslookup github.com | grep 'Address' | tail -n 1 | awk '{ print $2 }')
if [[ -z "$fastly_ip" || -z "$github_ip" ]]; then echo "IP 地址获取失败,脚本退出。" exit 1 fi
echo "Fastly IP: $fastly_ip" echo "GitHub IP: $github_ip"
cp /etc/hosts /etc/hosts.backup
{ echo "$fastly_ip github.global.ssl.fastly.net" && echo "$github_ip github.com" } >> /etc/hosts || { rollback_hosts; exit 1; }
systemctl start nscd || { echo "启动 nscd 服务失败,尝试回滚 hosts 文件。"; rollback_hosts; exit 1; }
nscd -i hosts || { echo "更新 DNS 缓存失败,尝试回滚 hosts 文件。"; rollback_hosts; exit 1; }
echo "GitHub hosts 更新成功。"
|