github 下载提速 一键脚本

# github 下载提速 一键脚本, 目前只支持 ubuntu (理论上装了 bash 的机器都支持)

## 依赖命令:

  curl

  egrep

  sed

### 大概原理:

爬取 github 的真实 ip 地址, 并写入 hosts 文件

### 建议使用 sudo 运行, 因为要修改 /etc/hosts 文件

## 脚本源码:

#!/bin/bash
#
# 功能:这个脚本是为了更新 github.com 的ip 并插入到 /etc/hosts 文件中, 以完成 git clone 的加速
# 作者: David Zhang
# Ver: 0.1
# 重要: 本脚本需要使用到 root 权限!如不信任可以 ctrl + c 打断运行
set -e
echo '重要: 本脚本需要使用到 root 权限!建议使用 sudo 运行! 如不信任可以 ctrl + c 打断运行...'
echo 'zsh 下 egrep 会失效,请切换到 bash 下运行!'

GITHUBIP=`curl https://github.com.ipaddress.com/ |egrep -o '<th>IP Address</th><td><ul class="comma-separated"><li>'[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+ | egrep  -o [0-9]+[.][0-9]+[.][0-9]+[.][0-9]+`

echo '获取到 github ip: ';echo $GITHUBIP

FASTLYIP=`curl https://fastly.net.ipaddress.com/github.global.ssl.fastly.net |egrep -o '<th>IP Address</th><td><ul class="comma-separated"><li>'[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+ | egrep  -o [0-9]+[.][0-9]+[.][0-9]+[.][0-9]+`

echo '获取到 github.global.ssl.fastly ip: ';echo $FASTLYIP

CODELOADIP=`curl https://github.com.ipaddress.com/codeload.github.com |egrep -o '<th>IP Address</th><td><ul class="comma-separated"><li>'[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+ | egrep  -o [0-9]+[.][0-9]+[.][0-9]+[.][0-9]+`

echo '获取到 codeload.github.com ip: ';echo $CODELOADIP

cp /etc/hosts ~/hosts.tmp;

sed -i '/github/d' ~/hosts.tmp;

echo '## github 加速用 hosts ##' >> ~/hosts.tmp
echo $GITHUBIP ' github.com' >> ~/hosts.tmp;
echo $FASTLYIP ' github.global.ssl.fastly.net' >> ~/hosts.tmp
echo $CODELOADIP ' codeload.github.com' >> ~/hosts.tmp

cp /etc/hosts{,.bak}
cp ~/hosts.tmp /etc/hosts
rm ~/hosts.tmp

echo 'done'
echo 'Now you got hosts like this:'

cat /etc/hosts

## 喜欢请点赞哦

猜你喜欢

转载自www.cnblogs.com/gettolive/p/11898231.html