编写自动修改hosts脚本来访问github

编写自动修改hosts脚本来访问github

由于手动修改 hosts 文件来实现 github 的访问比较麻烦,为此特意写了两个脚本(针对 linux 平台 和 windows 平台)来实现自动修改 github hosts

修改 hosts 的整体流程如下:

  • 下载 hosts
  • 备份系统原 hosts
  • 利用下载的 hosts 来更新系统 hosts

Linux 平台

  • 创建名为 github-hosts.shbash 脚本文件:

    touch github-hosts.sh
    
  • 赋予该脚本可执行权限:

    chmod +x github-hosts.sh
    
  • 向脚本写入如下内容:

    #!/bin/bash
    
    download_hosts_path=~/hosts   # hosts 下载路径
    hosts_path=/etc/hosts         # 系统 hosts 保存路径
    hosts_path_bak=/etc/hosts.bak # 系统 hosts 备份路径
    
    # 下载 hosts
    echo "########## downloading hosts about github to $download_hosts_path ##########"
    wget -O $download_hosts_path https://gitee.com/ineo6/hosts/raw/master/hosts
    
    # 备份 hosts
    echo "########## copying $hosts_path to $hosts_path_bak ##########"
    sudo cp $hosts_path $hosts_path_bak
    
    # 删除 hosts 中原有 github 相关配置
    echo "########## removing hosts about github in $hosts_path ##########"
    sudo sed -i ":begin; /# GitHub Host Start/,/# GitHub Host End/ { /# GitHub Host End/! { $! { N; b begin }; }; s/# GitHub Host Start.*# GitHub Host End//; };" $hosts_path
    sudo sh -c "sed -i '/^$/d' $hosts_path" # 删除系统 hosts 文件中的空行
    
    # 更新 hosts
    echo "########## updating hosts about github in $hosts_path ##########"
    sudo sh -c "sed -i '/^\\s*#.*$/d' $download_hosts_path" # 删除下载 hosts 文件中的注释
    sudo sh -c "sed -i '/^$/d' $download_hosts_path" # 删除下载 hosts 文件中的空行
    host_value=`cat $download_hosts_path` # 读取下载 hosts 文件内容
    host_value=`echo -e "\n# GitHub Host Start\n${host_value}\n# GitHub Host End"`
    sudo sh -c "echo \"$host_value\" >> $hosts_path" # 将新的 hosts 内容追加到系统 hosts 文件中
    
  • 执行该脚本:

    bash github-hosts.sh
    

Windows 平台

  • 创建名为 github-hosts.ps1powershell 脚本文件

  • 向该文件写入如下内容:

    $dowload_url = "https://gitee.com/ineo6/hosts/raw/master/hosts" # 下载地址
    $download_hosts_path = "$HOME\hosts"                            # hosts 下载路径
    $hosts_path = "C:\windows\system32\drivers\etc\hosts"           # 系统 hosts 保存路径
    $hosts_path_bak = "C:\windows\system32\drivers\etc\hosts.bak"   # 系统 hosts 备份路径
    $command = $MyInvocation.MyCommand.Definition                   # powershell 脚本路径
    
    # 判断用户是否为管理员
    function is_admin
    {
        $user = [Security.Principal.WindowsIdentity]::GetCurrent()
        return (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }
    
    # 下载 hosts
    function download_hosts 
    {
        Write-Host "########## downloading github hosts to $download_hosts_path ##########"
        $client = new-object System.Net.WebClient
        $client.DownloadFile($dowload_url, $download_hosts_path) 
    }
    
    # 备份 hosts
    function backups_hosts 
    {
        Write-Host "########## copying $hosts_path to $hosts_path_bak ##########"
        $original_hosts = Get-Content $hosts_path -Raw
        Set-Content $hosts_path_bak $original_hosts
    }
    
    # 删除 hosts 中原有 github 相关配置
    function remove_hosts 
    {
        Write-Host "########## removing github hosts in $hosts_path ##########"
        $original_hosts = Get-Content $hosts_path -Raw
        $original_hosts = $original_hosts -replace "# GitHub Host Start[\s\S]*?# GitHub Host End", ""
        $original_hosts = ($original_hosts -replace "(?m)^\s*`r`n", "").trim()
        Set-Content $hosts_path $original_hosts
    }
    
    # 更新 hosts
    function update_hosts 
    {
        Write-Host "########## updating github hosts in $hosts_path ##########"
        $new_hosts = Get-Content $download_hosts_path -Raw
        $new_hosts = $new_hosts -replace "#.*", ""
        $new_hosts = ($new_hosts -replace "(?m)^\s*`r`n", "").trim()
        $new_hosts = "", "# GitHub Host Start", $new_hosts, "# GitHub Host End" -join "`n"
        Add-Content $hosts_path $new_hosts
    }
    
    # 刷新 DNS
    function flush_dns 
    {
        Write-Host "########## refreshing DNS ##########"
        ipconfig /flushdns
    }
    
    $is_admin = is_admin
    
    # 非管理员则利用 Start-Process 命令来获取管理员权限
    if (!$is_admin) 
    {
        if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
            Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList $command
            exit
        }
    }
    
    download_hosts
    backups_hosts
    remove_hosts
    update_hosts
    flush_dns
    
  • 执行该脚本:

    .\github-hosts.ps1
    

最后

上述两个脚本文件已上传至 https://gitee.com/zhaoyangkun/github-hosts-shell,有需要的朋友可以自行下载。

在这里要特别感谢 ineo6 大佬提供的最新的 github hosts

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://cangmang.xyz/articles/1651891625337