一个一键修改IP设置的BAT脚本

发布时间:2021-09-20

  由于实验室的网络B类局域网固定IP,而寝室的是DHCP,做实验的时候还需要使用C类局域网固定IP。各种网络之间切换有些麻烦。因此在网上搜了一下BAT语言设定网卡的操作,希望能编写一个快捷修改网络设置的脚本。
这个脚本很多代码和技术参考了网友 D-小苏 的源码,在这里表示感谢!
::Author:Tony Chang
::2015.12.20
::Thanks D-小苏
@echo off
set Net_IP_Diy=10.10.40.53
set Net_MASK=255.255.252.0
set Net_gateway=10.10.40.1
set Net_dnsPra=123.125.81.6
set Net_dnsOrder=210.2.4.8
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

注册即送1000元现金券