# 定義: DFビットを立ててpingを実行する関数
function Test-MTU {
param (
[string]$Target,
[int]$StartMTU = 800,
[int]$EndMTU = 1000
)
$results = @()
for ($mtu = $StartMTU; $mtu -le $EndMTU; $mtu++) {
$pingResult = Test-Connection -ComputerName $Target -Count 1 -BufferSize $mtu -DontFragment -ErrorAction SilentlyContinue
if ($pingResult.StatusCode -eq 0) {
$results += "MTU: $mtu - Success"
} else {
$results += "MTU: $mtu - Failure"
}
}
return $results
}
# example.comに対してMTUを800から1000の範囲でテスト
$target = "example.com"
$mtuResults = Test-MTU -Target $target
# 全インターフェイスのMTUおよびMRU設定を取得
$interfaces = Get-NetAdapter | Select-Object -Property Name, MacAddress, LinkSpeed, InterfaceDescription
$mtuSettings = $interfaces | ForEach-Object {
$interfaceName = $_.Name
$mtu = Get-NetIPInterface -InterfaceAlias $interfaceName | Select-Object -ExpandProperty NlMtu
[PSCustomObject]@{
Interface = $interfaceName
MTU = $mtu
}
}
# 結果をファイルに書き出し
$logFile = "mtu-result.txt"
$mtuResults | Out-File -FilePath $logFile
Add-Content -Path $logFile -Value "`nCurrent MTU Settings:`n"
$mtuSettings | Format-Table -AutoSize | Out-String | Add-Content -Path $logFile
Write-Host "Results saved to $logFile"
# PowerShellスクリプト
function Test-MTU {
param (
[string]$hostname,
[int]$minMTU,
[int]$maxMTU
)
$logFile = "mtu-result.txt"
Remove-Item $logFile -ErrorAction SilentlyContinue
function Ping-Host {
param (
[string]$host,
[int]$size
)
$pingResult = Test-Connection -ComputerName $host -Count 4 -BufferSize $size -DontFragment -Quiet
return $pingResult
}
function BinarySearch-MTU {
param (
[string]$host,
[int]$low,
[int]$high
)
while ($low -le $high) {
$mid = [math]::Floor(($low + $high) / 2)
if (Ping-Host -host $host -size $mid) {
$low = $mid + 1
} else {
$high = $mid - 1
}
}
return $high
}
# 最適なMTUサイズを探す
$optimalMTU = BinarySearch-MTU -host $hostname -low $minMTU -high $maxMTU
"Optimal MTU size for $hostname is $optimalMTU" | Out-File -FilePath $logFile -Append
# 現在のすべてのインターフェイスのMTUおよびMRU設定を取得
$interfaces = Get-NetIPInterface
foreach ($interface in $interfaces) {
$interfaceInfo = "Interface: $($interface.InterfaceAlias), MTU: $($interface.NlMtu), MRU: $($interface.NlMtu)" # Note: Windows does not expose MRU directly, typically MRU == MTU
$interfaceInfo | Out-File -FilePath $logFile -Append
}
}
# 設定
$hostname = "example.com"
$minMTU = 800
$maxMTU = 1000
# 実行
Test-MTU -hostname $hostname -minMTU $minMTU -maxMTU $maxMTU