DNS解析速度检测脚本

SKYzjjzs3小时前网络相关1

修正后的脚本(保存为 .ps1 文件)

powershell
<#
.SYNOPSIS
    Test DNS resolution speed of common DNS servers and recommend the fastest two.
.DESCRIPTION
    This script tests a predefined list of DNS servers by resolving a fixed domain multiple times,
    calculates average response time (ms), sorts results, and outputs a table with recommendations.
.NOTES
    Requires PowerShell 3.0 or later (default on Windows 10).
    If a DNS server is unreachable, it will be skipped and marked as failed.
#>

# Define the test domain (can be modified)
$testDomain = "www.baidu.com"

# Number of tests per DNS (for averaging)
$testCount = 5

# List of DNS servers (Name and IP)
$dnsServers = @(
    @{ Name = "Google DNS"; IP = "8.8.8.8" },
    @{ Name = "Google DNS (alt)"; IP = "8.8.4.4" },
    @{ Name = "Cloudflare DNS"; IP = "1.1.1.1" },
    @{ Name = "Cloudflare DNS (alt)"; IP = "1.0.0.1" },
    @{ Name = "OpenDNS"; IP = "208.67.222.222" },
    @{ Name = "OpenDNS (alt)"; IP = "208.67.220.220" },
    @{ Name = "Quad9 DNS"; IP = "9.9.9.9" },
    @{ Name = "114 DNS"; IP = "114.114.114.114" },
    @{ Name = "Ali DNS"; IP = "223.5.5.5" },
    @{ Name = "Ali DNS (alt)"; IP = "223.6.6.6" },
    @{ Name = "Tencent DNS"; IP = "119.29.29.29" },
    @{ Name = "Baidu DNS"; IP = "180.76.76.76" }
)

Write-Host "Testing DNS resolution speed (domain: $testDomain, each DNS tested $testCount times)..." -ForegroundColor Cyan
Write-Host ""

# Store results
$results = @()

foreach ($dns in $dnsServers) {
    $dnsName = $dns.Name
    $dnsIP = $dns.IP

    Write-Host "Testing $dnsName ($dnsIP) ..." -NoNewline

    $times = @()
    for ($i = 1; $i -le $testCount; $i++) {
        try {
            $measure = Measure-Command {
                Resolve-DnsName -Name $testDomain -Server $dnsIP -ErrorAction Stop
            }
            $times += $measure.TotalMilliseconds
        }
        catch {
            Write-Host " FAILED" -ForegroundColor Red
            $times = $null
            break
        }
    }

    if ($times -and $times.Count -eq $testCount) {
        $avgTime = ($times | Measure-Object -Average).Average
        Write-Host " Avg: $([math]::Round($avgTime, 2)) ms" -ForegroundColor Green
        $results += [PSCustomObject]@{
            DNSName   = $dnsName
            DNSServer = $dnsIP
            AvgTimeMs = [math]::Round($avgTime, 2)
            Status    = "Success"
        }
    }
    else {
        Write-Host " FAILED (unreachable)" -ForegroundColor Red
        $results += [PSCustomObject]@{
            DNSName   = $dnsName
            DNSServer = $dnsIP
            AvgTimeMs = $null
            Status    = "Failed"
        }
    }
}

Write-Host ""
Write-Host "All tests completed. Results:" -ForegroundColor Cyan
Write-Host ""

# Filter successful results and sort by average time
$successResults = $results | Where-Object { $_.Status -eq "Success" } | Sort-Object AvgTimeMs

# Output table (only successful)
if ($successResults.Count -gt 0) {
    $successResults | Format-Table -Property DNSName, DNSServer, AvgTimeMs, Status -AutoSize
} else {
    Write-Host "No successful DNS tests." -ForegroundColor Red
}

# Recommend the fastest two
$top2 = $successResults | Select-Object -First 2
if ($top2.Count -ge 2) {
    Write-Host "Recommended fastest two DNS servers:" -ForegroundColor Yellow
    Write-Host "1. $($top2[0].DNSName) ($($top2[0].DNSServer)) - Avg time $($top2[0].AvgTimeMs) ms"
    Write-Host "2. $($top2[1].DNSName) ($($top2[1].DNSServer)) - Avg time $($top2[1].AvgTimeMs) ms"
}
elseif ($top2.Count -eq 1) {
    Write-Host "Only one DNS server succeeded. Recommended: $($top2[0].DNSName) ($($top2[0].DNSServer)) - Avg time $($top2[0].AvgTimeMs) ms"
}
else {
    Write-Host "No DNS servers available. Please check your network connection." -ForegroundColor Red
}
  1. 将上面的代码复制到记事本中。

  2. 保存文件时务必选择编码为“UTF-8 with BOM”或“ANSI”(推荐UTF-8 with BOM),文件名保存为 Test-DnsSpeed.ps1

  3. 在PowerShell中执行:

    powershell
    .\Test-DnsSpeed.ps1

    如果提示执行策略限制,可先运行:

    powershell
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

    然后再执行脚本。


标签: 教程

相关文章

UNRAID 添加 docker 加速

在 /etc/docker/daemon.json 文件中, registry-mirrors 部分添加加速地址例如:{   "registry-mirrors&quo...

Ubuntu 系统的语言修改为中文

打开“设置”应用:在 Ubuntu 桌面上,点击屏幕右上角的向下箭头图标(或系统设置图标),然后选择“Settings”(设置)。导航到“Region & Language”(区域和语言)设置...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。