github git clone 下載提速 一鍵腳本


# 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

## 喜歡請點贊哦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM