动态

详情 返回 返回

Github又卡住或打不開了,在沒有有效代理情況下最直接的解決方法

Github經常性的卡住或完全打不開常見原因和處理

網絡上常見可能原因

  • dns解析不到正確的ip
  • 國際出口擁塞導致高 RTT(200ms+)與大量丟包,導致網頁資源加載極慢或失敗
  • 偶爾會出現ssl證書驗證失敗問題

在沒有合規或者有效的代理情況下的最有效處理

  • 本機hosts文件手動添加域名解析
使用方法:
1. 執行獲取在windows上執行githubdnsip.bat或linux上執行githubdnsip.sh返回的ip
2. 打開本機的Hosts文件:
    windows系統: C:\Windows\System32\drivers\etc\hosts (需要管理員權限才能進行修改)
    Mac/Linux: 終端中輸入sudo vim /etc/hosts 進行
    添加解析記錄 (參考,具體依據獲取的ip進行設置):
    20.205.243.166 github.com 
     162.125.2.3 github.global.ssl.fastly.net
3. 刷新DNS緩存:
    Windows: ipconfig /flushdns
    Mac/Linux: sudo killall -HUP mDNSResponder
建議定期或出現問題時更新一次本機域名ip解析記錄

提供適用於windows和linux系統的命令行工具,便捷獲取github相關域名的解析ip地址

windows版本

  • 文件名:githubdnsip.bat
@echo off
setlocal EnableExtensions EnableDelayedExpansion

REM ================================================================
REM "GitHub 相關域名 DNS 查詢腳本 過濾解析器自身IP + 全局去重"
REM ------------------------------------------------
REM "用法"
REM   githubdnsip.bat
REM   githubdnsip.bat <dns>
REM   githubdnsip.bat <dns> <ipver>
REM "參數"
REM   "<dns>    遞歸DNS服務器  默認 208.67.222.222"
REM   "<ipver>  4=僅A, 6=僅AAAA, 46 或空=兩者"
REM "去重範圍 全局  所有域名 + A/AAAA 去重"
REM "若需每個域名單獨去重,可在域名循環裏重置 SEEN_IPS= "
REM ================================================================

set "DNS=%~1"
if "%DNS%"=="" set "DNS=208.67.222.222"
set "IPVER=%~2"
if "%IPVER%"=="" set "IPVER=4"

REM "全局已出現 IP 列表 前導空格便於精確匹配"
set "SEEN_IPS= "

REM "待查詢域名  github.githubassets.com user-images.githubusercontent.com avatars.githubusercontent.com objects.githubusercontent.com  api.github.com  raw.githubusercontent.com"
set "DOMAINS=github.com github.global.ssl.fastly.net codeload.github.com"

echo Using resolver: %DNS%
echo Query types: %IPVER%
echo.

for %%D in (%DOMAINS%) do (
  echo ===== %%D =====
  echo.
  REM "如果想按域名單獨去重,在此處加  set \"SEEN_IPS= \""

  echo %IPVER% | findstr "4" >nul && call :DoLookup %%D A
  echo %IPVER% | findstr "6" >nul && call :DoLookup %%D AAAA

  echo.
)

goto :EOF

:DoLookup
REM "%1 = domain, %2 = type A / AAAA"
nslookup -type=%2 %1 %DNS% > "%TEMP%\__gh_ns.tmp" 2>nul
if errorlevel 1 (
  echo [%2] %1 : lookup failed
  del /q "%TEMP%\__gh_ns.tmp" >nul 2>nul
  goto :EOF
)

set "IN_ADDR_BLOCK=0"
for /f "usebackq delims=" %%L in ("%TEMP%\__gh_ns.tmp") do (
  set "LINE=%%L"

  if /I "!LINE:~0,7!"=="Server:" (
    REM skip
  ) else (
    if /I "!LINE:~0,5!"=="Name:" (
      echo [%2] !LINE!
    ) else (
      REM "處理 Address 與 Addresses"
      echo(!LINE!| findstr /R /B /I "Address:  *" >nul && (
        call :PrintAddress %2 "!LINE!"
      )
      echo(!LINE!| findstr /R /B /I "Addresses:  *" >nul && (
        call :PrintAddress %2 "!LINE!"
        set "IN_ADDR_BLOCK=1"
      )

      REM "處理 Addresses 後續續行 前導空格 + IP"
      if !IN_ADDR_BLOCK! EQU 1 (
        echo(!LINE!| findstr /R "^[ ][ ]*[0-9A-Fa-f][0-9A-Fa-f:.]*$" >nul
        if errorlevel 1 (
          set "IN_ADDR_BLOCK=0"
        ) else (
          call :PrintAddress %2 "!LINE!"
        )
      )
    )
  )
)

del /q "%TEMP%\__gh_ns.tmp" >nul 2>nul
goto :EOF

:PrintAddress
REM "%1 = 記錄類型A/AAAA, %2 = 原始行"
set "REC=%~1"
set "RAW=%~2"

set "WORK=%RAW%"
:TrimLead
if defined WORK if "!WORK:~0,1!"==" " set "WORK=!WORK:~1!" & goto :TrimLead

set "HEAD="
set "REST="
for /f "tokens=1* delims=:" %%a in ("!WORK!") do (
  set "HEAD=%%a"
  set "REST=%%b"
)

if /I "!HEAD!"=="Address" (
  set "CAND=!REST!"
) else if /I "!HEAD!"=="Addresses" (
  set "CAND=!REST!"
) else (
  set "CAND=!WORK!"
)

set "TMP=!CAND!"
:TrimLead2
if defined TMP if "!TMP:~0,1!"==" " set "TMP=!TMP:~1!" & goto :TrimLead2

for /f "tokens=1" %%i in ("!TMP!") do set "IPONLY=%%i"

REM "過濾 DNS 解析器自身 IP"
if /I "!IPONLY!"=="%DNS%" (
  goto :EOF
)

REM "去重 如果 SEEN_IPS 中已有該 IP 則跳過"
echo !SEEN_IPS! | findstr /I /C:" !IPONLY! " >nul && (
  REM duplicate skip
) 

 (
  set "SEEN_IPS=!SEEN_IPS!!IPONLY! "
  echo [%REC] !RAW!
)

goto :EOF
  • 執行結果:
C:\Users\xxx\Desktop>githubdnsip.bat

Using resolver: 208.67.222.222
Query types: 4

===== github.com =====

[REC] Address:  20.205.243.166

===== github.global.ssl.fastly.net =====

[REC] Address:  162.125.2.3

===== codeload.github.com =====

[REC] Address:  20.205.243.165

ubuntu版本

  • 文件名:githubdnsip.sh
    對腳本賦予可執行權限: sudo chmod +x githubdnsip.sh
#!/usr/bin/sh
# ================================================================
# GitHub 域名 DNS 查詢(可指定解析器 + A/AAAA 選擇 + 全局去重 + 統一輸出)
# 用法:
#   ./githubdnsip.sh.sh
#   ./githubdnsip.sh.sh <dns>
#   ./githubdnsip.sh.sh <dns> <ipver>
# 參數:
#   <dns>    遞歸DNS服務器 (默認 208.67.222.222)
#   <ipver>  4=僅A, 6=僅AAAA, 46 兩者,或空=4
# 説明:
#   - 依賴: dig (dnsutils / bind-utils)
#   - 輸出格式: "[A] Address: 140.82.114.4" / "[AAAA] Address: 2606:50c0:8000::154"
#   - 全局去重(IPv4/IPv6),並過濾等於解析器自身 IP 的條目
# ================================================================

DNS="${1:-208.67.222.222}"
IPVER="${1:-4}"

if ! command -v dig >/dev/null 2>&1; then
  echo "需要安裝 dig (dnsutils/bind-utils)。例如: Ubuntu/Debian: sudo apt-get install dnsutils; CentOS/RHEL: sudo yum install bind-utils" >&2
  exit 1
fi

case "$IPVER" in
  4) TYPES="A" ;;
  6) TYPES="AAAA" ;;
  46|64|'') TYPES="A AAAA" ;;
  *) echo "未知 ipver: $IPVER, 使用 A" >&2; TYPES="A" ;;
esac

DOMAINS="github.com github.global.ssl.fastly.net codeload.github.com"

echo "Using resolver: $DNS"
echo "Query types: $IPVER"
echo

# 生成並用 awk 去重與過濾(見下方 awk)
{
  for d in $DOMAINS; do
    printf "===== %s =====\n\n" "$d"
    for t in $TYPES; do
      # 直接用 dig +short 輸出純 IP(可能多行),逐行包裝為統一格式
      dig +short @"$DNS" -t "$t" "$d" +time=3 +tries=1 \
        | awk -v T="$t" '
            /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ { printf "[%s] Address: %s\n", T, $0; next }
            /^[0-9A-Fa-f:]+$/                  { printf "[%s] Address: %s\n", T, $0; next }
          '
    done
    printf "\n"
  done
} | awk -v dns="$DNS" '
  # 透傳分隔行與空行
  /^===== / { print; next }
  /^$/      { print; next }

  # 僅對 IP 行做過濾與去重;格式固定為: [A|AAAA] Address: <IP>
  /^\[(A|AAAA)\] Address: / {
    ip = $3
    if (ip == dns) next
    if (!(ip in seen)) { seen[ip] = 1; print }
    next
  }

  { print }
'

  • 執行結果
xx@gogodev:~/Desktop$ ./githubdnsip.sh 
Using resolver: 208.67.222.222
Query types: 4

===== github.com =====

[A] Address: 20.205.243.166

===== github.global.ssl.fastly.net =====

[A] Address: 185.45.6.57

===== codeload.github.com =====

[A] Address: 20.205.243.165
user avatar
0 用户, 点赞了这篇动态!

发布 评论

Some HTML is okay.