寻求可替代Unix工具Ethtool的Windows以太网自动协商工具及对应命令行实现方案
Hey there! Let's break down how to replicate that Unix ethtool script's functionality on Windows—since Windows doesn't have a direct drop-in replacement for ethtool, we'll use built-in tools to get the job done.
ethtool for Ethernet Autonegotiation First, the two go-to built-in tools for managing Ethernet interface settings on Windows are:
netsh: The classic command-line utility that works in both Command Prompt and PowerShell. It's been around forever and handles most basic network config tasks.- PowerShell Cmdlets:
Get-NetAdapterandSet-NetAdapterAdvancedPropertyare more modern, granular tools that make it easier to target specific adapters and properties.
Replicating Your Unix Script's Behavior
Your original script loops through all network interfaces, turns on autonegotiation for each, and prints a confirmation message. Here's how to do this in Windows:
Option 1: Command Prompt (CMD) Script
CMD doesn't handle interface loops as smoothly as Unix shells, but we can hack together a working script with netsh and a for loop:
@echo off setlocal enabledelayedexpansion :: Loop through all Ethernet interfaces (skip non-physical/loopback adapters) for /f "tokens=2 delims=:" %%i in ('netsh interface show interface ^| findstr /i "ethernet"') do ( :: Trim leading whitespace from the interface name set "iface=%%i" set "iface=!iface:~1!" echo Turning on autonegotiation for !iface! :: Enable link-level autonegotiation (speed/duplex) netsh interface set interface "!iface!" autonegotiate=enabled :: If you also need to enable IP autoconfiguration (DHCP), add this line: :: netsh interface ipv4 set interface "!iface!" autoconfiguration=enabled ) endlocal
Heads up: The exact property name might vary depending on your network adapter's driver. If
autonegotiate=enableddoesn't work, check your adapter's advanced properties in Control Panel to find the correct parameter name, then adjust thenetshcommand accordingly.
Option 2: PowerShell Script (More Reliable)
PowerShell is way better at handling network adapters, so this is the recommended approach:
# Grab all physical Ethernet adapters (skips loopback and virtual adapters) $ethernetAdapters = Get-NetAdapter | Where-Object { $_.PhysicalMediaType -eq "802.3" } foreach ($adapter in $ethernetAdapters) { Write-Host "Enabling autonegotiation on $($adapter.Name)" # Turn on link-level autonegotiation (speed/duplex) Set-NetAdapterAdvancedProperty -Name $adapter.Name -DisplayName "Auto Negotiation" -DisplayValue "Enabled" # If you need to enable DHCP for IP config, uncomment this line: # Set-NetIPInterface -InterfaceAlias $adapter.Name -Dhcp Enabled }
Pro tip: If "Auto Negotiation" isn't the right display name for your adapter, run
Get-NetAdapterAdvancedProperty -Name $adapter.Nameto list all available properties for your hardware, then use the correct name from that output.
Important Notes
- Run as Admin: Both scripts need to be executed with Administrator privileges—otherwise, you'll hit access denied errors when trying to modify interface settings.
- Adapter Compatibility: Some older network adapters might not support autonegotiation, or might use a different property name (like "Speed & Duplex" set to "Auto"). Always double-check your adapter's specific settings if the commands don't work.
内容的提问来源于stack exchange,提问作者sanjanaraju_inde

