运营商会在每天的一个时间进行重新拨号,这种情况对于linux来说可能没问题,但是windows 不会释放ipv6地址,出现地址并存

对于临时地址过多的情况来说,关闭ipv6临时地址可能会起到作用(但是这会涉及到一些安全问题):

netsh interface ipv6 set privacy state=disabled store=active
netsh interface ipv6 set privacy state=disabled store=persistent
netsh interface ipv6 set global randomizeidentifiers=disabled store=active
netsh interface ipv6 set global randomizeidentifiers=disabled store=persistent

如果路由是可控的,将RA interval调整到一个较低的值即可。

但是有可能会出现,不仅是临时地址,ipv6地址本身也被过期未释放的地址抢占,这时候可以监控网口的ipv6数量,超过即重启协议栈。

编写powershell脚本如下(个数为2的):

# Replace 'Ethernet' with your interface name
$interface = '以太网实例 0'

$ipv6Addresses = Get-NetIPAddress -InterfaceAlias $interface -AddressFamily IPv6

if ($ipv6Addresses.Count -gt 2) {
    Write-Output "More than one IPv6 address detected. Restarting IPv6..."

    # Disable IPv6
    Set-NetAdapterBinding -Name $interface -ComponentID ms_tcpip6 -Enabled $false

    # Enable IPv6
    Set-NetAdapterBinding -Name $interface -ComponentID ms_tcpip6 -Enabled $true
} else {
    Write-Output "Only one IPv6 address detected. No action needed."
}

或者可以编写cmd命令,但是只能重启网卡:

@echo off
setlocal enabledelayedexpansion

:: Replace 'Ethernet' with your interface name
set "interface=以太网实例 0"

for /f "delims=" %%i in ('netsh interface ipv6 show address "%interface%" ^| find /i "地址 " /c') do set count=%%i

echo %count%
if %count% GTR 2 (
    echo More than two IPv6 address detected. Restarting the network interface...
    :: Disable IPv6
    Set-NetAdapterBinding -Name $interface -ComponentID ms_tcpip6 -Enabled $false

    :: Enable IPv6
    Set-NetAdapterBinding -Name $interface -ComponentID ms_tcpip6 -Enabled $true
) else (
    echo Only two IPv6 address detected. No action needed.
)

endlocal

这之后,将这个脚本添加到windows定时任务中,在计算机管理->系统工具->任务计划程序 中,进行如下添加

勾选 安全选项->不管用户是否登录都要运行/使用最高权限运行

触发器可按如下配置

操作 启动程序

程序或脚本选择 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

添加参数 -command “. ‘c:\test.ps1′”