Created
August 24, 2025 10:38
-
-
Save wbmins/80ca30dbcede797cff32d5b111223497 to your computer and use it in GitHub Desktop.
设置网络为旁路由 192.168.1.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| :: 网络清理与静态IP设置脚本 | |
| :: 请以管理员身份运行 | |
| echo 正在执行网络清理与旁路由配置...... | |
| echo. | |
| :: ================ 第一步:清理 Profiles 下的所有子项 ================ | |
| echo 1/2 正在清理 Profiles 下的所有网络子项... | |
| for /f "delims=" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" 2^>nul ^| findstr /r "\\[^\\]*$"') do ( | |
| reg delete "%%a" /f >nul 2>&1 | |
| ) | |
| echo 清理完成:所有网络配置子项已重置。 | |
| echo. | |
| :: ================ 第二步:修改静态 IP 配置为旁路由 ================ | |
| echo 2/2 正在设置静态 IP 地址,配置为旁路由... | |
| :: 初始化网卡名称 | |
| set "interface=" | |
| :: 尝试通过 netsh 获取已连接的有线网卡(包含“以太网”或“Ethernet”) | |
| for /f "skip=3 tokens=*" %%a in ('netsh interface show interface 2^>nul') do ( | |
| echo %%a | findstr /i "以太网 Ethernet" >nul && ( | |
| for /f "tokens=4" %%n in ("%%a") do set "interface=%%n" | |
| ) | |
| ) | |
| :: 如果未找到,尝试匹配任意状态下的“以太网” | |
| if not defined interface ( | |
| for /f "skip=3 tokens=4" %%n in ('netsh interface show interface 2^>nul ^| findstr /i "以太网"') do ( | |
| set "interface=%%n" | |
| goto :found_interface | |
| ) | |
| ) | |
| :found_interface | |
| :: 如果还是没找到,使用默认名称 | |
| if not defined interface set "interface=以太网" | |
| echo 检测到网卡: %interface% | |
| :: 设置静态 IP | |
| echo 正在设置 IP 地址: 192.168.1.2,子网掩码: 255.255.255.0,网关: 192.168.1.5 | |
| netsh interface ip set address name="%interface%" static 192.168.1.2 255.255.255.0 192.168.1.5 >nul 2>&1 | |
| :: 检查是否设置成功 | |
| if %errorlevel% equ 0 ( | |
| echo IP 设置成功。 | |
| ) else ( | |
| echo IP 设置失败:请检查网卡名称是否正确。 | |
| ) | |
| :: 设置 DNS | |
| echo 正在设置 DNS 服务器: 192.168.1.5 | |
| netsh interface ip set dns name="%interface%" static 192.168.1.5 >nul 2>&1 | |
| if %errorlevel% equ 0 ( | |
| echo DNS 设置成功。 | |
| ) else ( | |
| echo DNS 设置失败。 | |
| ) | |
| echo 网络设置完成: | |
| echo. | |
| echo profiles : reset done | |
| echo network : %interface% | |
| echo ipaddr : 192.168.1.2 | |
| echo netmask : 255.255.255.0 | |
| echo gateway : 192.168.1.5 | |
| echo dns : 192.168.1.5 | |
| echo. | |
| echo 按任意键退出... | |
| pause >nul |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| :: 网络清理 + 启用 DHCP | |
| :: 请以管理员身份运行 | |
| echo 正在执行网络重置... | |
| echo. | |
| :: ================ 第一步:清理 Profiles 下所有子项 ================ | |
| echo 1/2 正在清理网络配置缓存... | |
| for /f "delims=" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" 2^>nul ^| findstr /r "\\[^\\]*$"') do ( | |
| reg delete "%%a" /f >nul 2>&1 | |
| ) | |
| echo 清理完成: profiles reset done | |
| echo. | |
| :: ================ 第二步:启用 DHCP(自动获取IP) ================ | |
| echo 2/2 正在设置网络为自动获取... | |
| :: 默认网卡名称 | |
| set "interface=以太网" | |
| :: 尝试从 netsh 检测真实网卡名称 | |
| for /f "skip=3 tokens=4" %%n in ('netsh interface show interface 2^>nul ^| findstr /i "以太网"') do ( | |
| set "interface=%%n" | |
| goto :interface_found | |
| ) | |
| :interface_found | |
| :: 启用 DHCP 获取 IP | |
| netsh interface ip set address name="%interface%" dhcp >nul 2>&1 | |
| :: 启用 DHCP 获取 DNS | |
| netsh interface ip set dns name="%interface%" dhcp >nul 2>&1 | |
| echo 网络设置完成: | |
| echo. | |
| echo profiles : reset done | |
| echo network : %interface% | |
| echo ipconfig : dhcp enabled | |
| echo dns : dhcp enabled | |
| echo. | |
| echo 网络已设置为自动获取IP和DNS。 | |
| echo 按任意键退出... | |
| pause >nul |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment