自动修改本机IP地址的bat代码
(编辑:jimmy 日期: 2024/11/15 浏览:3 次 )
代码比较多,经过小编测试,在2008 r2上无法正常执行,其实bat设置ip就是几个命令,无外乎加了一些判断与获取原来网络配置的一些信息,代码越复杂越容易出问题
@echo off setlocal enabledelayedexpansion title 修改本机IP地址 by:小小沧海20130409 :init cls&echo ——————————————自动修改本机IP地址—————————————— rem 系统版本,值可为Windows7或是WindowsXP,或是auto(表示自动获取) set SYSVER=auto rem 要更改的网卡名称,auto表示自动获取第一块“以太网适配器” set ETH=auto rem IP来源,值仅为两个static和dhcp,ques表示询问,由使用者填写 rem 静态IP请填写static,从网关自动获取IP请填写dhcp set IPSOURCE=ques rem 要改成的IP地址,ques同上 set IPADDR=ques rem 要改成的子网掩码,ques同上 set MASK=ques rem 要使用的默认网关,ques同上 set GATEWAY=ques rem DNS模式,值仅为两个static和dhcp rem 静态DNS请填写static,从网关自动获取DNS请填写dhcp set DNSSOURCE=ques rem 要使用的首选DNS,ques同上 set DNS1=ques rem 要使用的备用DNS,ques同上 set DNS2=ques set LOG=%TEMP%\changeIP_log.txt echo 运行日期:%date% %time%>%LOG% :start rem ===============使用者填写参数值======================= rem 自动获取系统版本,结果为 Windows7 或是 WindowsXP(只测试了这两个系统) if "%SYSVER%"=="auto" ( set /p=正在自动获取系统版本...<nul for /f "skip=1 tokens=2-3 delims= " %%i in ('wmic os get caption') do set SYSVER=%%i%%j if /i "!SYSVER!"=="Windows7" ( echo 成功![Win7] ) else ( if /i "!SYSVER!"=="WindowsXP" ( echo 成功![WinXP] ) else ( echo [!SYSVER!] echo 【注意】非Win7和XP系统不保证能执行成功!&pause>nul ) ) ) rem 自动获取网卡名称 if "%ETH%"=="auto" ( echo 正在自动获取网络适配器信息... set index=0 set select=1 for /f "skip=3 tokens=4* delims= " %%i in ('netsh interface ipv4 show interfaces^|find /i /v "Loopback"') do ( set /a index=!index!+1 set ethname=%%j echo [!index!]!ethname! ) if !index!==1 ( set ETH=!ethname! ) else ( if !index! GTR 1 ( :select set /p=请选择要设置的网卡编号:<nul set select=0&set /p select= if /i !select! LSS 1 goto select if /i !select! GTR !index! goto select set index=0 for /f "skip=3 tokens=4* delims= " %%i in ('netsh interface ipv4 show interfaces^|find /i /v "Loopback"') do ( set /a index=!index!+1 if !index!==!select! ( set ETH=%%j ) ) )) if "!ETH!"=="auto" ( echo 自动获取网卡名称失败,请右键编辑本批处理,手动填写网卡名称!&pause>nul&exit ) else ( rem set/p=[!ETH!]<nul echo 成功! ) ) :quesIP if "%IPSOURCE%"=="ques" ( echo →请填写【IP地址来源】^(值仅为两个static和dhcp,直接回车为static^) set /p IPSOURCE= if "!IPSOURCE!"=="ques" set IPSOURCE=static if /i "!IPSOURCE!" NEQ "static" (if /i "!IPSOURCE!" NEQ "dhcp" ( set IPSOURCE=static echo IP来源填写错误,将变更为static模式 pause>nul )) ) if /i "%IPSOURCE%"=="dhcp" goto quesDNS if "%IPADDR%"=="ques" ( echo →请填写【IP地址】^(直接回车为192.168.1.100^) set /p IPADDR= if "!IPADDR!"=="ques" set IPADDR=192.168.1.100 ) if "%MASK%"=="ques" ( echo →请填写【子网掩码】^(直接回车为255.255.255.0^) set /p MASK= if "!MASK!"=="ques" set MASK=255.255.255.0 ) if "%GATEWAY%"=="ques" ( echo →请填写【默认网关】^(直接回车为192.168.1.1^) set /p GATEWAY= if "!GATEWAY!"=="ques" set GATEWAY=192.168.1.1 ) :quesDNS if "%DNSSOURCE%"=="ques" ( echo →请填写【DNS来源】^(值仅为两个static和dhcp,直接回车为static^) set /p DNSSOURCE= if "!DNSSOURCE!"=="ques" set DNSSOURCE=static if /i "!DNSSOURCE!" NEQ "static" (if /i "!DNSSOURCE!" NEQ "dhcp" ( set DNSSOURCE=static echo DNS来源填写错误,将变更为static模式 pause>nul )) ) if /i "%DNSSOURCE%"=="dhcp" goto checkInfo if "%DNS1%"=="ques" ( echo →请填写【首选DNS地址】^(直接回车为8.8.8.8^) set /p DNS1= if "!DNS1!"=="ques" set DNS1=8.8.8.8 ) if "%DNS2%"=="ques" ( echo →请填写【备用DNS地址】^(直接回车为8.8.4.4^) set /p DNS2= if "!DNS2!"=="ques" set DNS2=8.8.4.4 ) :checkInfo cls echo 即将应用以下配置: call :showInfo echo 请确认信息是否正确,输入Y继续,输入N退出,输入Q显示本机网络信息 set choose=nul&set /p choose= if /i "%choose%"=="nul" goto checkInfo if /i "%choose%"=="N" exit if /i "%choose%"=="Q" call :getInfo & pause & goto checkInfo if /i "%choose%" NEQ "Y" goto checkInfo echo ★注意★请关闭防火墙或允许所有弹出的安全软件提示,否则无法成功执行! :changeIP rem 通过dhcp删除原有IP配置 echo →设置"%ETH%"的IP源为DHCP,以删除原有IP地址 %LOG% netsh -c interface ip set address name="%ETH%" source=dhcp %LOG% if /i "%IPSOURCE%"=="static" ( echo →设置IP为"%IPADDR%",掩码为"%MASK%",网关为"%GATEWAY%" %LOG% netsh -c interface ip set address name="%ETH%" source=static address="%IPADDR%" mask="%MASK%" gateway="%GATEWAY%" gwmetric=1 %LOG% ) rem 删除原有DNS配置 echo →删除原有DNS配置 %LOG% netsh -c interface ip delete dns "%ETH%" all %LOG% if /i "%DNSSOURCE%"=="static" ( echo →设置首选DNS为%DNS1% %LOG% netsh -c interface ip add dns name="%ETH%" addr="%DNS1%" index=1 %LOG% echo →设置备用DNS为%DNS2% %LOG% netsh -c interface ip add dns name="%ETH%" addr="%DNS2%" index=2 %LOG% rem ↑此处可继续增加多个DNS服务器地址 ) else (if /i "%DNSSOURCE%"=="dhcp" ( echo →设置DNS为DHCP模式 %LOG% netsh -c interface ip set dns name="%ETH%" dhcp %LOG% )) :end cls rem echo 【要设定的信息】 rem call :showInfo echo 【当前本机信息】 call :getInfo echo ====================================== echo 如果上下一致则说明修改成功! echo 如果不一致则请查看日志文件! echo 输入L查看日志文件,输入E退出程序。 set choose=nul&set /p choose= if /i "%choose%"=="L" start %LOG%&goto end if /i "%choose%"=="E" exit if /i "%choose%"=="nul" goto end echo 程序执行结束,按任意键退出... pause>nul exit :showInfo echo 【本机系统】:%SYSVER% echo 【网卡名称】:%ETH% echo 【IP来源 】:%IPSOURCE% if "%IPSOURCE%"=="static" ( echo 【IP地址 】:%IPADDR% echo 【子网掩码】:%MASK% echo 【默认网关】:%GATEWAY% ) echo 【DNS来源 】:%DNSSOURCE% if "%DNSSOURCE%"=="static" ( echo 【首选DNS 】:%DNS1% echo 【备用DNS 】:%DNS2% ) rem goto :eof等于返回return goto :eof :getInfo netsh -c interface ip show address name="%ETH%" netsh -c interface ip show dns name="%ETH%" goto :eof :windows7 ============================================== 接口 "本地连接" 的配置 DHCP 已启用: 否 IP 地址: 192.168.1.253 子网前缀: 192.168.1.0/24 (掩码 255.255.255.0) 默认网关: 192.168.1.1 网关跃点数: 1 InterfaceMetric: 20 接口 "本地连接" 的配置 静态配置的 DNS 服务器: 8.8.8.8 8.8.4.4 用哪个前缀注册: 只是主要
BAT批处理复杂环境下的IP地址修改设置
@echo off mode con cols=70 lines=38 color a title D-小苏-学习-Bat-快速修改本地IP地址 :ShowReadme cls echo. echo *********************学习-Bat-快速修改本地IP地址********************** echo. echo 实现功能(以下情况下测试通过): echo 1.多连接名称:自动获取网络连接名称 echo 2.多连接属性:有线连接,无线连接,虚拟连接 echo 3.多操作系统:Windows XP,Windows 7 / 8.1 echo 4.多网络连接:无线连接2个,有线连接1个,VMware连接2个 echo 5.区域的DNS:判断计算机可以连接互联网则采用当前DNS设置 echo 6.空格名称:VMware Virtual Ethernet Adapter for VMnet1 echo 7.优化设置:优化选择步骤,添加选择序号,保存配置内容 echo 8.设置方法:可转换Netsh interface ip set 和WMIC nicconfig echo 9.获取优化:判断网关值,存储有效的网关值以打开路由器设置页 echo. echo *********************学习-Bat-快速修改本地IP地址********************** ping -n 1 127.1 >nul :Set_localTemp setlocal enabledelayedexpansion ver | find "6." > NUL &&(cls&echo. &echo -------------------------------当前系统为 Windows 7以上系统 &Set "WindowsVer=Windows 7" &goto :Set_NetConnectionID) cls&echo. &echo -------------------------------当前系统不是 Windows 7以上系统 &Set "WindowsVer=WindowsXP" &goto :Set_NetConnectionID :Set_NetConnectionID rem 获取设置网络连接名称 cls echo. echo *************选择网络连接名称*****************IN %WindowsVer%********** 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 %%i_%%j适配器_Index_Name.txt echo. echo 适配器_ID_Index: %%i echo 适配器_名_NName: %%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 ************************选择网络连接名称***************************** (for /f "tokens=1* delims=:" %%a in ('findstr /n .* 适配器_Index_Name.txt') do echo ID_%%a_%%b)>适配器_ID_Index_Name.txt REM 生成选择结构,添加序号ID_,避免纯查找数字序号会关键词的查找重复 for /f "tokens=2-4 delims=_" %%a in (适配器_ID_Index_Name.txt) do ( set NetConID=%%a set NetConnectionID_Index=%%b set NetConnectionName_Index=%%c echo 适配器ID:!NetConID! 标号Index:!NetConnectionID_Index! 名称:!NetConnectionName_Index! ) echo *************选择网络连接名称*****************IN %WindowsVer%*********** :Set_Net_Ask Echo. echo. set /p NetConID=选择需要配置 适配器 对应 数字ID : if /i "%NetConID%" == "q" exit echo. echo. set NetConnectionName_Index= for /f "tokens=2-4 delims=_" %%a in ('findstr "ID_%NetConID%" 适配器_ID_Index_Name.txt') do ( setlocal enabledelayedexpansion set NetConnectionID_Index=%%b set NetConnectionName_Index=%%c echo 当前选择适配器ID:!NetConID! 标号Index:!NetConnectionID_Index! 名称:!NetConnectionName_Index! ) if "%NetConnectionName_Index%" == "" (echo 选择选项超出范畴 ... &ping -n 3 127.1>nul &goto Set_Net_Ask) else (set net_interface=%NetConnectionName_Index% &echo 名称:!net_interface! ) if exist 适配器_Index_Name.txt (del /a /f /s /q 适配器_Index_Name.txt) else echo 适配器_Index_Name.txt文件夹不存在 if exist 适配器_ID_Index_Name.txt (del /a /f /s /q 适配器_ID_Index_Name.txt) else echo 适配器_ID_Index_Name.txt文件夹不存在 ping -n 1 127.1>nul :Set_RamdonIP Rem 如果计算机可以连接互联网,判断网络DNS、网关是否可用,可以连接互联网则只改IP,启用当前区域DNS... Rem 获得可上网IP前三位,设置为随机IP前三位 if "%Net_IP%" == "" (echo 设置Net_IP &set Net_IP=192.168.1.128) for /f "tokens=1-4 delims=/." %%a in ("%Net_IP%") do (set IPa_=%%a.%%b.%%c) set /a IPb_=%random%%%255+3 set "net_IPnew=%IPa_%.%IPb_%" echo 随机IP前三位[%IPa_%] 生成随机IP [%net_IPnew%] ping 127.0.0.1 -n 1 >nul 1>nul ping www.baidu.com &echo %errorlevel% if %errorlevel% equ 1 goto:Set_Net_Set_NoHave if %errorlevel% equ 0 goto:Set_Net_Set_Have :Set_Net_Set_Have Echo 当前计算机可以连通互联网...启用当前区域DNS... set Net_Ping=1 set Net_IP_Diy=%net_IPnew% Rem 当前IP地址 ping -n 1 127.0.0.1>nul goto DiyCHOICE :Set_Net_Set_NoHave Echo 当前计算机无法连通互联网... set Net_Ping=0 set Net_IP_Diy=%net_IPnew% set "Net_MASK=255.255.255.0" set "Net_gateway=192.168.1.1" set "Net_dnsPra=202.100.192.68" set "Net_dns2=202.100.199.8" ping -n 1 127.0.0.1>nul goto DiyCHOICE :DiyCHOICE cls echo. echo ****************************IP快速切换程序*************************** echo. echo 当前选择网络连接: echo ID:%NetConID% 名称:%net_interface% echo. echo. echo --------------1.修改为随机IP---------------------%Net_IP_Diy% echo --------------2.设自动获取IP---------------------XXX.XXX.X.XXX echo --------------3.手动设网关IP---------------------192.XXX.X.XXX echo --------------4.查看网络设置---------------------%Net_IP% echo --------------5.设置路由器页---------------------%Net_gateway% echo. echo --------------r.重启网卡-------------------------x.退出程序--- echo. echo. echo **** IN %WindowsVer% *******************************Tools By S34205**** echo. echo. echo. if "%Net_Ping%" == "1" (echo 当前计算机可以连接互联网,启用当前区域DNS...) else (echo 当前计算机无法连接互联网,可能需要设置您区域DNS....) echo 随机的IP:[%Net_IP_Diy%] echo 子网掩码:[%Net_MASK%] echo 默认网关:[%Net_gateway%] echo 首选DNS:[%Net_dnsPra%] IF defined dns2 echo 备用DNS:[%Net_dns2%] echo. echo. ping -n 1 127.1 >nul echo. :Diysetchoice set /p SetChoice_C= 请输入操作: Echo. Echo. IF NOT "%SetChoice_C%"=="" SET SetChoice_C=%SetChoice_C:~0,1% if /i "%SetChoice_C%"=="1" goto Static_ip if /i "%SetChoice_C%"=="2" goto DHCP_ip if /i "%SetChoice_C%"=="3" goto Diy_ip if /i "%SetChoice_C%"=="4" goto Look_ip if /i "%SetChoice_C%"=="5" goto Set_Moden_Web if /i "%SetChoice_C%"=="r" goto rebootnet if /i "%SetChoice_C%"=="x" goto END echo 输入的选择超出范围... ping -n 3 127.1>nul goto DiyCHOICE :Static_ip echo. echo 设置随机IP:[%Net_IP_Diy%] ... REM netsh interface ip set address name="%net_interface%" source=static addr=%Net_IP_Diy% mask=%net_MASK% REM echo 设置IP成功... REM Echo 设置网关... REM netsh interface ip set address name="%net_interface%" gateway=%Net_gateway% gwmetric=1 REM echo 设置DNS... REM netsh interface ip set dns name="%net_interface%" static addr=%Net_dnsPra% register=PRIMARY REM netsh interface ip add dns name="%net_interface%" addr=%Net_dns2% index=2 wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Net_IP_Diy%),(%net_MASK%) wmic nicconfig where index=%NetConnectionID_Index% call setgateways(%Net_gateway%) wmic nicconfig where index=%NetConnectionID_Index% call setdnsdomain(%Net_dnsPra%) wmic nicconfig where index=%NetConnectionID_Index% call SetDNSServerSearchOrder(%Net_dns2%) goto Set_IP_Done :DHCP_ip echo. echo 正在设置IP为自动获取,请等待... REM echo 设置IP中... REM netsh interface ip set address name="%net_interface%" source=dhcp REM echo 设置网关成功...设置DNS中... REM netsh interface ip set dns "%net_interface%" source=dhcp wmic path Win32_NetworkAdapterConfiguration.index=%NetConnectionID_Index% call enabledhcp wmic path Win32_NetworkAdapterConfiguration.index=%NetConnectionID_Index% call SetDNSServerSearchOrder() goto Set_IP_Done :Diy_ip set/p Net_IP_Diy=请输入IP地址 XXX.XXX.XXX.XXX: set/p Net_gateway=请输入网关地址 XXX.XXX.XXX.XXX: set/p Net_dnsPra=请输入主dns地址 XXX.XXX.XXX.XXX: REM netsh interface ip set address name="%net_interface%"source=static addr=%Net_IP_Diy% mask=255.255.255.0 REM netsh interface ip set address name="%net_interface%" gateway=%Net_gateway% gwmetric=0 REM netsh interface ip set dns name="%net_interface%" source=static addr=%Net_dnsPra% register=PRIMARY wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Net_IP_Diy%),(%net_MASK%) wmic nicconfig where index=%NetConnectionID_Index% call setgateways(%Net_gateway%) wmic nicconfig where index=%NetConnectionID_Index% call setdnsdomain(%Net_dnsPra%) wmic nicconfig where index=%NetConnectionID_Index% call SetDNSServerSearchOrder(%Net_dns2%) goto Set_IP_Done :Look_ip Echo. if "%LookNum%" == "2" ( Echo 打开:控制面板-网络连接... ping -n 1 127.1>nul start control ncpa.cpl Echo 说明:开启环境延迟第二次Bat运行适配器属性不会改变,所以第二次次信息查看需要手动查看属性... &Echo. echo,前次%net_interface%地址的IP:%IPAddress% &echo,前次%net_interface%子网掩码:%IPSubnet% &echo,前次%net_interface%默认网关:%DefaultIPGateway% ping -n 6 127.1>nul &goto Set_IP_Done ) set LookNum=1 set /a LookNum+=1 Echo 查看网络连接%net_interface%的属性: set "w1=wmic nic where "NetConnectionID="%net_interface%"" get Index" for /f %%a in ('%w1% ^| findstr /b [0-9]') do set "n=%%a" set "w2=wmic nicconfig where "Index='%n%'" get DefaultIPGateway^,IPAddress^,IPSubnet" for /f tokens^=1-2delims^={^" %%a in ('%w2% /value^|find "."')do set "%%a%%b" echo,%net_interface%地址的IP:%IPAddress% echo,%net_interface%子网掩码:%IPSubnet% echo,%net_interface%默认网关:%DefaultIPGateway% ping -n 6 127.1>nul goto Set_IP_Done :rebootnet echo 重启网卡中...请稍等... ping -n 3 127.1>nul netsh interface set interface "%net_interface%" disabled netsh interface set interface "%net_interface%" enable goto Set_IP_Done :Set_Moden_Web Echo 需要修改%net_interface%IP为网关地址段[%Net_gateway%]... set /p SetChoice_Web=是(1) 否(2) 返回(3) : Echo. IF NOT "%SetChoice_Web%"=="" SET SetChoice_Web=%SetChoice_Web:~0,1% if /i "%SetChoice_Web%"=="1" goto Set_web_ip if /i "%SetChoice_Web%"=="2" goto Set_no_web_ip if /i "%SetChoice_Web%"=="2" goto DiyCHOICE echo 输入的选择超出范围... ping -n 3 127.1>nul goto :Set_Moden_Web :Set_web_ip Rem 获得网关前三位,设置为IP前三位,如果不存在则创建 if "%Net_gateway%" == "" (echo 设置Net_gateway &set Net_gateway=192.168.1.1) for /f "tokens=1-4 delims=/." %%a in ("%Net_gateway%") do (set Set_web_IPa_=%%a.%%b.%%c) set /a Set_web_IPb_=%random%%%255+3 set "Set_web_net_IPnew=%Set_web_IPa_%.%Set_web_IPb_%" echo IP前三位:%Set_web_IPa_% 后一位:%Set_web_IPb_% echo 正在设置%net_interface%为网关IP[%Set_web_net_IPnew%]...... wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Set_web_net_IPnew%),(%net_MASK%) echo 已经设置%net_interface%为网关IP[%Set_web_net_IPnew%]...... goto :Set_no_web_ip :Set_no_web_ip Echo 当前网关为:%Net_gateway% ,正在打开 http://%Net_gateway% .... ping %Net_gateway% >nul start "%programfiles%\Internet Explorer\iexplore" http://%Net_gateway% ping -n 3 127.1>nul goto :Set_IP_Done :Set_IP_Done echo. echo OK,设置完成... ping -n 1 127.1>nul echo OK,任务完成,返回选择菜单... ping -n 1 127.1>nul GOTO DiyCHOICE :end echo **********************退出......................********************* Endlocal ping -n 3 127.1>nul exit
继续分享一个
@echo off for /f "delims=" %%a in ('wmic NIC where "NetEnabled='TRUE'" get MACAddress^,NetConnectionID /value^|find "="') do set %%a for /f "delims=" %%a in ('wmic NICCONFIG where "MACAddress='%MACAddress%'" get IPAddress /value^|find "="') do set %%a for /f "delims={," %%a in ("%IPAddress%") do set ip=%%~a for /f "tokens=1-4 delims=." %%a in ("%ip%") do set last=%%d echo;%ip% echo;%last% echo;%NetConnectionID% netsh interface ipv4 set address name="本地连接" addr=192.168.1.%last% mask=255.255.255.0 gwmetric=30 netsh interface ipv4 add address name="本地连接" addr=192.168.2.%last% mask=255.255.255.0 gateway=192.168.2.1 gwmetric=1 exit
如果是小编自己用的话直接用
netsh interface ipv4 add address name="本地连接" addr=192.168.2.8 mask=255.255.255.0 gateway=192.168.2.1 gwmetric=1
需要事先把这些ip、子网掩码、网关设置好,执行运行就可以了,方便粗暴。
下一篇:DOS批处理不支持将UNC 路径作为当前目录的解决方法