搶占IP修復(fù)記錄)
修復(fù) Windows 網(wǎng)絡(luò)共享ICS把192.168.137.1釘死在斷開網(wǎng)卡上導(dǎo)致無法分配給目標(biāo)網(wǎng)卡“以太網(wǎng) 2”的問題。問題所在這臺電腦曾開啟過 Windows 網(wǎng)絡(luò)共享ICS它把網(wǎng)關(guān)地址192.168.137.1固定分配給了內(nèi)置網(wǎng)卡“以太網(wǎng)”該網(wǎng)卡沒插線處于斷開狀態(tài)。因此想把192.168.137.1設(shè)到 USB 網(wǎng)卡“以太網(wǎng) 2”時系統(tǒng)報“對象已存在”分配失敗“以太網(wǎng) 2”拿不到地址只能自我分配169.254.x.x的無效地址重啟電腦也無效因?yàn)?ICS 服務(wù)開機(jī)又會把地址釘回“以太網(wǎng)”。解決步驟已完成診斷確認(rèn)SharedAccessICS服務(wù)在運(yùn)行192.168.137.1被它占在斷開的“以太網(wǎng)”上釋放地址Remove-NetIPAddress強(qiáng)制從“以太網(wǎng)”移除殘留 IP直接分配、重啟網(wǎng)卡均無效因?yàn)?ICS 會立刻搶回重新分配netsh interface ip set address name25 static 192.168.137.1 255.255.255.0成功設(shè)到“以太網(wǎng) 2”驗(yàn)證192.168.137.1狀態(tài)Preferred開發(fā)板192.168.137.76ping 通。本機(jī)網(wǎng)卡信息??ifIndex 因機(jī)器而異換機(jī)先重查。網(wǎng)卡ifIndex說明以太網(wǎng)23內(nèi)置 Intel I219-V未插線ICS 殘留占用192.168.137.1以太網(wǎng) 225Realtek USB 網(wǎng)卡目標(biāo)網(wǎng)卡連接開發(fā)板WLAN13Wi-Fi 上網(wǎng)網(wǎng)卡ICS 共享的出口netsh的name參數(shù)同時接受網(wǎng)卡名和 ifIndex用 ifIndex 可避免腳本中中文網(wǎng)卡名的編碼問題。診斷指令# 查看網(wǎng)卡與 ifIndex、連接狀態(tài)Get-NetAdapter|Format-Table-AutoSize ifIndex,Name,Status,InterfaceDescription# 查看各網(wǎng)卡 IPv4 地址及狀態(tài)重點(diǎn)看 AddressState 是否為 DeprecatedGet-NetIPAddress-AddressFamily IPv4|Format-Table-AutoSize InterfaceAlias,IPAddress,PrefixLength,PrefixOrigin,AddressState,InterfaceIndex# 經(jīng)典方式查看全部網(wǎng)卡配置對應(yīng) ipconfig /allnetsh interface ipv4 show config# 確認(rèn) ICSSharedAccess服務(wù)是否在運(yùn)行Get-ServiceSharedAccess|Format-Table-AutoSize Name,Status,StartType# 查詢 ICS 固定占用的網(wǎng)關(guān)地址Get-ItemPropertyHKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters|Format-ListScopeAddress,ScopeAddressBackup判斷要點(diǎn)目標(biāo) IP 出現(xiàn)在其他網(wǎng)卡哪怕已斷開且AddressState Deprecated加上SharedAccess Running、ScopeAddress 192.168.137.1即可確認(rèn)是ICS 釘住的殘留地址——這正是netsh報“對象已存在”的根源。無效路徑踩坑記錄避免重復(fù)嘗試# ① 直接分配 → 報“對象已存在”netsh interface ipsetaddress name25 static 192.168.137.1 255.255.255.0# ② 把殘留網(wǎng)卡改回 DHCP → ICS 立刻又把 192.168.137.1 分回去netsh interface ipsetaddress name23 dhcp# ③ 禁用/啟用殘留網(wǎng)卡 → ICS 依舊重新?lián)屨糄isable-NetAdapter-InterfaceIndex 23-Confirm:$falseStart-Sleep-Seconds 4Enable-NetAdapter-InterfaceIndex 23-Confirm:$false以上三種方式全部無效SharedAccess服務(wù)每次都會主動把ScopeAddress重新分配到它綁定的網(wǎng)卡上。有效修復(fù)指令核心需管理員權(quán)限# ① 強(qiáng)制移除以太網(wǎng)(ifIndex23) 上的殘留 IP —— 唯一有效的釋放方式Remove-NetIPAddress-InterfaceIndex 23-IPAddress 192.168.137.1-Confirm:$false# ② 立即將 IP 分配給以太網(wǎng) 2(ifIndex25)不給 ICS 搶回的時間窗口netsh interface ipsetaddress name25 static 192.168.137.1 255.255.255.0# ③ 清除以太網(wǎng) 2上遺留的靜態(tài) DNS殘留過 192.168.137.1 的指向Set-DnsClientServerAddress-InterfaceIndex 25-ResetServerAddresses①②兩步必須連續(xù)執(zhí)行等價的 PowerShell 原生寫法New-NetIPAddress -InterfaceIndex 25 -IPAddress 192.168.137.1 -PrefixLength 24。驗(yàn)證指令# 確認(rèn)以太網(wǎng) 2已持有 192.168.137.1 且狀態(tài)為 PreferredGet-NetIPAddress-InterfaceIndex 25-AddressFamily IPv4|Format-Table-AutoSize InterfaceAlias,IPAddress,PrefixLength,AddressState# 確認(rèn)以太網(wǎng)已回落到正常的 169.254 自動地址Get-NetIPAddress-InterfaceIndex 23-AddressFamily IPv4-ErrorAction SilentlyContinue# 連通性測試對端設(shè)備 IP 按實(shí)際替換ping 192.168.137.76預(yù)期結(jié)果以太網(wǎng) 2 192.168.137.1/24Preferredping 全部回復(fù)、0% 丟包。一鍵修復(fù)腳本自動重試版保存為fix_ip.ps1以管理員身份運(yùn)行# fix-ics-ip.ps1 - auto-detect version, MUST run as Administrator# Fixes: target IP stuck on a stale adapter (ICS-held), object already exists error# Usage:# .\fix-ics-ip.ps1 (defaults: 192.168.137.1 / Realtek USB)# .\fix-ics-ip.ps1 -TargetIP 192.168.137.1 -TargetDescPattern Realtek USBparam([string]$TargetIP192.168.137.1,[string]$TargetDescPatternRealtek USB)# --- 1. Locate target adapter by hardware description (ifIndex is NOT stable across reboots) ---$targetGet-NetAdapter|Where-Object{$_.InterfaceDescription-like*$TargetDescPattern*}|Select-Object-First 1if(-not$target){Write-Host[ERROR] No adapter matching *$TargetDescPattern*. Available adapters:-ForegroundColor RedGet-NetAdapter|Format-TableifIndex,Name,Status,InterfaceDescription-AutoSizeexit1}Write-Host[INFO] Target adapter:$($target.Name)(ifIndex$($target.ifIndex))# --- 2. Release the IP from any OTHER adapter that currently holds it (ICS re-grabs, so retry) ---for($round 1;$round-le3;$round){$holdersGet-NetIPAddress-IPAddress$TargetIP-ErrorAction SilentlyContinue|Where-Object{$_.InterfaceIndex-ne$target.ifIndex}if(-not$holders){break}foreach($hin$holders){Write-Host[FIX ] Round$round: removing$TargetIPfrom $($h.InterfaceAlias)Remove-NetIPAddress-InterfaceIndex$h.InterfaceIndex-IPAddress$TargetIP-Confirm:$false-ErrorAction SilentlyContinue}Start-Sleep-Seconds 2}# --- 3. Assign to target adapter reset stale DNS ---netsh interface ipsetaddress name$($target.ifIndex)static$TargetIP255.255.255.0 21|Out-NullStart-Sleep-Seconds 3Set-DnsClientServerAddress-InterfaceIndex$target.ifIndex-ResetServerAddresses-ErrorAction SilentlyContinue# --- 4. Verify ---$okGet-NetIPAddress-InterfaceIndex$target.ifIndex-AddressFamily IPv4-ErrorAction SilentlyContinue|Where-Object{$_.IPAddress-eq$TargetIP-and$_.AddressState-neDeprecated}if($ok){Write-Host[ OK ]$TargetIPis now on $($target.Name) (Preferred)-ForegroundColor Green}else{Write-Host[FAIL] IP not assigned. Current state:-ForegroundColor RedGet-NetIPAddress-InterfaceIndex$target.ifIndex-AddressFamily IPv4-ErrorAction SilentlyContinue|Format-TableInterfaceAlias,IPAddress,AddressState-AutoSize}使用方法# 管理員 PowerShell 中運(yùn)行默認(rèn)目標(biāo)192.168.137.1 / Realtek USB 網(wǎng)卡.\fix_ip.ps1# 或顯式指定參數(shù).\fix_ip.ps1-TargetIP 192.168.137.1-TargetDescPatternRealtek USB實(shí)際調(diào)試以太網(wǎng)適配器 以太網(wǎng): 媒體狀態(tài)............: 媒體已斷開連接 連接特定的 DNS 后綴.......: 以太網(wǎng)適配器 以太網(wǎng) 2: 連接特定的 DNS 后綴.......: 本地鏈接 IPv6 地址........: fe80::a7f4:3de0:3973:9eb3%25 自動配置 IPv4 地址.......: 169.254.4.194 子網(wǎng)掩碼............: 255.255.0.0 默認(rèn)網(wǎng)關(guān).............:PSC:\system-patch\ics.\fix_ip.ps1-TargetIP 192.168.137.1-TargetDescPatternRealtek USB[INFO]Target adapter: 以太網(wǎng) 2(ifIndex25)[FIX ]Round 1 : removing 192.168.137.1from以太網(wǎng)[OK]192.168.137.1 is now on以太網(wǎng) 2(Preferred)以太網(wǎng)適配器 以太網(wǎng) 2: 連接特定的 DNS 后綴.......: 本地鏈接 IPv6 地址........: fe80::a7f4:3de0:3973:9eb3%25 IPv4 地址............: 192.168.137.1 子網(wǎng)掩碼............: 255.255.255.0 默認(rèn)網(wǎng)關(guān).............:普通終端提權(quán)運(yùn)行彈 UAC 確認(rèn)Start-Processpowershell-Verb RunAs-Wait-ArgumentList -NoProfile -ExecutionPolicy Bypass -File D:\完整路徑\fix-ics-ip.ps1回滾命令恢復(fù)原狀# 以太網(wǎng) 2恢復(fù) DHCP 自動獲取netsh interface ipsetaddress name25 dhcp# PowerShell 等價寫法Set-NetIPInterface-InterfaceIndex 25-Dhcp EnabledRemove-NetIPAddress-InterfaceIndex 25-IPAddress 192.168.137.1-Confirm:$false防復(fù)發(fā)在圖形界面完成一次WinR→ncpa.cpl右鍵WLAN→ 屬性 →共享選項(xiàng)卡勾選“允許其他網(wǎng)絡(luò)用戶通過此計算機(jī)的 Internet 連接來連接”家庭網(wǎng)絡(luò)連接下拉框選“以太網(wǎng) 2”→ 確定完成后 ICS 的192.168.137.1由系統(tǒng)自動維護(hù)在“以太網(wǎng) 2”上重啟不再復(fù)發(fā)對端設(shè)備將網(wǎng)關(guān)和 DNS 指向192.168.137.1即可通過電腦共享上網(wǎng)。經(jīng)驗(yàn)教訓(xùn)PowerShell 5.1 中文亂碼運(yùn)行含中文的.ps1會按 ANSI 讀取導(dǎo)致亂碼報錯——腳本內(nèi)用ifIndex代替中文網(wǎng)卡名本次因此將name以太網(wǎng) 2改為name25“對象已存在”排查netsh報該錯時先用Get-NetIPAddress全局查該 IP 是否掛在其他網(wǎng)卡上包括已斷開的網(wǎng)卡ICS 釘住的地址改 DHCP、禁用/啟用網(wǎng)卡都無效只有Remove-NetIPAddress強(qiáng)制移除成功。