| @echo off |
| title 网络工具 |
| color 3E |
| ::preset IP |
| set Net_IP_Diy=192.168.1.1 |
| set Net_MASK=255.255.255.0 |
| set Net_gateway=192.168.1.1 |
| |
| ::preset DNS |
| set Net_dnsPra= 192.168.1.1 |
| set Net_dnsOrder= 192.168.1.2 |
| |
| :: BatchGotAdmin |
| :------------------------------------- |
| |
| >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" |
| |
| REM --> If error flag set, we do not have admin. |
| if '%errorlevel%' NEQ '0' ( |
| echo Requesting administrative privileges... |
| goto UACPrompt |
| ) else ( |
| goto gotAdmin |
| ) |
| |
| :UACPrompt |
| echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" |
| echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" |
| |
| "%temp%\getadmin.vbs" |
| exit /B |
| |
| :gotAdmin |
| if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) |
| pushd "%CD%" |
| CD /D "%~dp0" |
| :-------------------------------------- |
| |
| :start |
| |
| echo. |
| echo. |
| echo. |
| :Set_localTemp |
| setlocal enabledelayedexpansion |
| |
| :Set_NetConnectionID |
| |
| echo. |
| echo 选择网络连接名称 |
| echo. |
| set "str=DefaultIPGateway^,IPAddress^,DNSServerSearchOrder^,IPSubnet^,DHCPEnabled" |
| for /f "skip=1tokens=1*" %%i in ('wmic Nic where "Manufacturer<>'Microsoft' and NetConnectionStatus='2'" get Index^,NetConnectionID^') do ( |
| for /f "tokens=1* delims=:" %%j in ("%%j") do ( |
| set $%%i=%%j |
| |
| echo. |
| echo 适配器_名_NName: %%j |
| set "NetConnectionName=%%j" |
| for /f "delims=" %%a in ('powershell "'%%j'.Trim()"') do set "NetConnectionName=%%a" |
| |
| for /f "tokens=1-3delims={}," %%a in ('wmic Nicconfig where "Index='%%i'" get %str% /value') do ( |
| for /f %%c in (%%c) do if /i "%%a" == "DNSServerSearchOrder=" set "DNS2=%%c" |
| for /f %%c in ("%%a%%~b") do set "%%c" |
| ) |
| if /i "!DHCPEnabled!" == "TRUE" ( |
| echo 适配器_IP_模式 : 自动 |
| ) else echo 适配器_IP_模式 : 手动 |
| IF not defined Net_IP set Net_IP=!IPAddress! |
| echo 适配器_IP_地址 : !IPAddress! 预置_IP_地址 : !Net_IP_Diy! |
| IF not defined Net_MASK set Net_MASK=!IPSubnet! |
| echo 适配器_子网掩码: !IPSubnet! 预置_子网掩码: !Net_MASK! |
| IF not defined Net_gateway set Net_gateway=!DefaultIPGateway! |
| echo 适配器_默认网关: !DefaultIPGateway! 预置_默认网关: !Net_gateway! |
| IF not defined Net_dnsPra set Net_dnsPra=!DNSServerSearchOrder! |
| echo 适配器_首选_DNS: !DNSServerSearchOrder! 预置_首选_DNS: !Net_dnsPra! |
| IF defined dns2 set Net_dns2=!DNS2! &echo 适配器_备用_DNS :!DNS2! 预置_备用_DNS: !Net_dns2! |
| ) |
| ) |
| echo. |
| |
| echo. |
| echo 请输入数字选择下一步操作 |
| echo 1:设置 IP |
| echo 2:清空 IP |
| echo 3:DNS固定 |
| echo 4:DNS清空 |
| echo ============= 按0直接退出 ============= |
| set /P var=":" |
| if %var%==1 goto ipstatic |
| if %var%==2 goto ipdhcp |
| if %var%==3 goto dnsstatic |
| if %var%==4 goto dnsdhcp |
| if %var%==0 exit |
| echo 错误:非法输入! |
| goto start |
| |
| :ipstatic |
| echo "%NetConnectionName%" |
| echo 设置IP... |
| netsh interface ipv4 set address name="%NetConnectionName%" source=static %Net_IP_Diy% %net_MASK% %Net_gateway% |
| ipconfig /flushdns |
| echo **IP设置为%Net_IP_Diy%,子网掩码设置为%net_MASK%,默认网关设置为%Net_gateway%,设置成功** |
| echo ------------------------------------------ |
| goto start |
| |
| :ipdhcp |
| netsh interface ip set address name="%NetConnectionName%" source=dhcp |
| ipconfig /flushdns |
| echo **IP设置为DHCP获取,设置成功** |
| echo ------------------------------------------ |
| goto start |
| |
| :dnsstatic |
| echo 设置DNS... |
| netsh interface ipv4 set dns name="%NetConnectionName%" static addr=%Net_dnsPra% register=PRIMARY |
| netsh interface ipv4 add dns name="%NetConnectionName%" addr=%Net_dnsOrder% index=2 |
| ipconfig /flushdns |
| echo **首选DNS设置为%Net_dnsPra%,备用DNS设置为%Net_dnsOrder%,设置成功** |
| echo ------------------------------------------ |
| goto start |
| |
| :dnsdhcp |
| echo 清除DNS... |
| netsh interface ip delete dns "%NetConnectionName%" all |
| ipconfig /flushdns |
| echo **DNS设置为自动获取,设置成功** |
| echo ------------------------------------------ |
| goto startCOPY |