通用方法实现了,需要sed.exe和choice.exe及devcon64.exe,可根据需要修改 。
@rem
@ECHO OFF
rem &
(PUSHD "%~DP0")&(REG QUERY "HKU\S-1-5-19">NUL 2>&1)||(powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"&&EXIT)
setlocal enabledelayedexpansion
rem color 2e
title 快速修复网络小地球修改网卡IPv4校验和
set regpath=HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\
set logfile=%~dp0IPInfo.log
set tmpfile=%~dp0tmp.log
set tmpfile2=%~dp0tmp2.log
rem ---
if exist %tmpfile% del %tmpfile%
:Select_Card
rem echo 正在预处理,请稍候片刻,马上就来......
set "nstr="
set /a m=0
rem 抓取全部网卡名称
rem ^"^,把双引号和逗号当作分隔符,会忽略掉,相当于删除
for /f tokens^=1^,2^,3^,4delims^=^"^, %%a in ('Getmac /v /nh /fo csv') do (
set /a m+=1
set nstr=!nstr!!m!
rem echo m=!m!
rem echo nstr=!nstr!
rem pause
set "name!m!=%%a"
set "card!m!=%%b"
set "mac!m!=%%c"
rem csmc,第四项:传输名称
set "csmc!m!=%%d"
rem set "name=%name:~1,-1%"
)
if %m% == 0 ( echo,&& echo 找不到网卡哦!&& goto end2 )
rem echo !m!
rem wknum:网卡数
set /a wknum=!m!
rem echo %wknum%
set nstr=!nstr!0
rem echo %nstr%
rem pause
rem Getmac /v /nh /fo csv
rem "蓝牙网络连接 2","Bluetooth Device (Personal Area Network) #2","70-66-55-E9-D7-30","媒体已断开连接"
rem "以太网 3","Realtek PCIe GbE Family Controller","BC-E9-2F-88-6B-71","\Device\Tcpip_{BA51047F-9C20-43C2-831E-CB960FDA385B}"
rem "WLAN 2","Realtek RTL8822CE 802.11ac PCIe Adapter","70-66-55-E9-D7-31","媒体已断开连接"
cls
echo,
echo 快速修复网络小地球修改网卡IPv4校验和
echo,
echo 网卡列表
echo =======================================================================
for /l %%i in (1,1,%wknum%) do (
echo %%i、!name%%i!,!card%%i!,!mac%%i!
)
echo 0、退出
echo =======================================================================
rem 只有一张网卡时不用输入选择了
if %wknum% equ 1 (
set /a Select_Card=1
goto con
)
if %wknum% gtr 9 ( echo,&echo 项目列表数多于9,暂时不处理!&goto end )
rem choice不能放在if中,不然errorlevel都是0
set /a n=%wknum%+1
set miao=18
choice /c %nstr% /n /t %miao% /d 0 /m "请选择网卡[1-%wknum%,%miao%秒后自动选择0]:"
if errorlevel %n% exit
set Select_Card=%errorlevel%
rem echo %Select_Card%
rem pause
rem
rem 可能需要先在设备管理器中显示隐藏的设备,把不存在的网络适配器删除
rem set idstr=ASUS XG-C100C 10G PCI-E Network Adapter
set card=!card%Select_Card%!
set idstr=!card%Select_Card%!
rem echo "%idstr%"
rem set "sbdm=PCI\VEN_10EC^&DEV_8168^&SUBSYS_012310EC\^&REV_15\716B88661900000000: Realtek PCIe GbE Family Controller"
rem PCI\VEN_10EC&DEV_8168&SUBSYS_012310EC&REV_15\716B88661900000000: Realtek PCIe GbE Family Controller
REM 下面是USB网卡的
REM USB\VID_2357&PID_0601\000000000000: TP-LINK Gigabit Ethernet USB Adapter
rem 下面是蓝牙
rem BTH\MS_BTHPAN\6&2690852&0&2: Bluetooth Device (Personal Area Network)
for /f "tokens=1,2,3,* delims=:\" %%i in (%tmpfile%) do set sbdm=%%k
if "%sbdm%" == "" ( echo,&echo 该设备的设备代码不存在,无法处理,任意键返回!& pause >nul & goto Select_Card )
rem echo "%sbdm%"
rem pause
echo %idstr%|findstr "#" >nul
if %errorlevel% equ 0 (
set idstr=!idstr:~0,-3!
)
rem idstr后面有#2之类的,如ASUS XG-C100C 10G PCI-E Network Adapter #2,注册表中会没有,所以搜索不出来,要删除最后3个字符
rem REG QUERY %regpath% /s|findstr "0318}\\00" >%tmpfile%
REG QUERY %regpath% /s >%tmpfile%
rem 删除空行
sed -i "/^$/d" %tmpfile%
rem 取得数字行数
find /i /n "%idstr%" %tmpfile%|find /i "%idstr%" >%tmpfile2%
sed -i "s/\[//g;s/\]//g" %tmpfile2%
rem %tmpfile2%
rem %tmpfile%
rem pause
rem for /f "tokens=1,2* delims=" %%i in (%tmpfile2%) do (
for /f "tokens=1,2*" %%i in (%tmpfile2%) do (
set linenum=%%i
)
rem echo %linenum%
set /a linenum-=1
rem echo %linenum%
set n=0
for /f "tokens=*" %%i in (%tmpfile%) do (
set /a n+=1
rem echo !n!
rem echo %%i
rem pause
if !n!==%linenum% (
set str=%%i
goto gogo
)
)
:gogo
rem echo %str%
rem pause
rem *IPChecksumOffloadIPv4默认是3,Rx & Tx Enable,0是Disable
rem
set zhi=0
set miao=5
choice /c 123 /n /t %miao% /d 1 /m "请选择:1禁用,2恢复默认,3退出(%miao%秒后默认输入1):"
rem echo %errorlevel%
rem pause
if errorlevel 3 exit
if errorlevel 2 set zhi=3
@reg add "%str%" /v "*IPChecksumOffloadIPv4" /t REG_SZ /d "%zhi%" /f>nul
rem 需要网卡禁用和启用一下
echo,&echo 正在禁用网卡[%card%]...
devcon64 disable *%sbdm%*
echo,&echo 正在启用网卡[%card%]...
devcon64 enable *%sbdm%*
rem pause
:end
rem 鸣叫一声
for /f "delims=h" %%a in ('cmd /u /c echo 标') do echo %%a
set miao=8
echo 稍候%miao%秒自动关闭本窗口,或自行手动关闭...
ping 127.0.0.1 -n %miao% >nul