Win10动静态IP地址切换bat脚本

日期: 2020-01-06 16:23

CHCP 65001>nul

@echo off

set Net_IP_Diy=192.168.31.11
set Net_MASK=255.255.252.0
set Net_gateway=192.168.31.111
set Net_dnsPra=192.168.31.111
set Net_dnsOrder=114.114.114.114
title 一键切换IP地址

:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>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 *********************快速修改本地IP地址**********************
ping -n 1 127.1 >nul
:Set_localTemp
setlocal enabledelayedexpansion

:Set_NetConnectionID
rem 获取设置网络连接名称
cls
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 "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! Net_IP_地址 : !Net_IP!
IF not defined Net_MASK set Net_MASK=!IPSubnet!
Rem 判断是否存在值,以解决之后Net_MASK赋值为空的情况,保证Net_MASK存在值
echo 适配器_子网掩码: !IPSubnet! Net_子网掩码: !Net_MASK!
IF not defined Net_gateway set Net_gateway=!DefaultIPGateway!
echo 适配器_默认网关: !DefaultIPGateway! Net_默认网关: !Net_gateway!
IF not defined Net_dnsPra set Net_dnsPra=!DNSServerSearchOrder!
echo 适配器_首选_DNS: !DNSServerSearchOrder! Net_首选_DNS: !Net_dnsPra!
IF defined dns2 set Net_dns2=!DNS2! &echo 适配器_配用_DNS :!DNS2! Net_配用_DNS: !Net_dns2!
)
)
echo.

echo ============请输入数字选择下一步操作:============
echo ============1:ip设置为%Net_IP_Diy% =============
echo ============2:设置为DHCP自动获取ip =============
echo ============ 按q直接退出 =============
set /P var=":"
if %var%==1 goto ip10
if %var%==2 goto ipdhcp
if %var%==q exit
echo 输入的选择超出范围...
ping -n 3 127.1>nul
goto start

:ip10
echo %NetConnectionName%
echo 设置IP...
netsh interface ipv4 set address name=%NetConnectionName% source=static %Net_IP_Diy% %net_MASK% %Net_gateway%
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

echo **IP设置为%Net_IP_Diy%,设置成功**
echo ^-^ ^-^ ^-^ ^-^ ^-^ ^-^ ^-^ ^-^
echo ------------------------------------------
ping -n 1 127.1>nul
goto start

:ipdhcp
netsh interface ip set address name=%NetConnectionName% source=dhcp
netsh interface ip delete dns %NetConnectionName% all
ipconfig /flushdns
echo **IP设置为DHCP获取,设置成功**
echo ------------------------------------------
ping -n 1 127.1>nul
goto start


Win10动静态IP地址切换bat脚本


注意:

拷贝保存成bat,直接运行,输入1,2,q切换或退出。可能会遇到安全卫士的拦截,允许操作即可。

相关新闻