这篇文章给大家介绍使用bat脚本文件怎么自动修改本机IP地址,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
@echo offsetlocal enabledelayedexpansion:initcls&echo ——————————————自动修改本机IP地址——————————————rem 系统版本,值可为Windows7或是WindowsXP,或是auto(表示自动获取)set SYSVER=autorem 要更改的网卡名称,auto表示自动获取第一块“以太网适配器”set ETH=autorem IP来源,值仅为两个static和dhcp,ques表示询问,由使用者填写rem 静态IP请填写static,从网关自动获取IP请填写dhcpset IPSOURCE=quesrem 要改成的IP地址,ques同上set IPADDR=quesrem 要改成的子网掩码,ques同上set MASK=quesrem 要使用的默认网关,ques同上set GATEWAY=quesrem DNS模式,值仅为两个static和dhcprem 静态DNS请填写static,从网关自动获取DNS请填写dhcpset DNSSOURCE=quesrem 要使用的首选DNS,ques同上set DNS1=quesrem 要使用的备用DNS,ques同上set DNS2=quesset LOG=%TEMP%\changeIP_log.txtecho 运行日期:%date% %time%>%LOG%:startrem ===============使用者填写参数值=======================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 成功! )):quesIPif "%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 quesDNSif "%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):quesDNSif "%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 checkInfoif "%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):checkInfoclsecho 即将应用以下配置:call :showInfoecho 请确认信息是否正确,输入Y继续,输入N退出,输入Q显示本机网络信息set choose=nul&set /p choose=if /i "%choose%"=="nul" goto checkInfoif /i "%choose%"=="N" exitif /i "%choose%"=="Q" call :getInfo & pause & goto checkInfoif /i "%choose%" NEQ "Y" goto checkInfoecho ★注意★请关闭防火墙或允许所有弹出的安全软件提示,否则无法成功执行!:changeIPrem 通过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%)):endclsrem echo 【要设定的信息】rem call :showInfoecho 【当前本机信息】call :getInfoecho ======================================echo 如果上下一致则说明修改成功!echo 如果不一致则请查看日志文件!echo 输入L查看日志文件,输入E退出程序。set choose=nul&set /p choose=if /i "%choose%"=="L" start %LOG%&goto endif /i "%choose%"=="E" exitif /i "%choose%"=="nul" goto endecho 程序执行结束,按任意键退出...pause>nulexit:showInfoecho 【本机系统】:%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等于返回returngoto :eof:getInfonetsh -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 offmode con cols=70 lines=38color atitle D-小苏-学习-Bat-快速修改本地IP地址:ShowReadmeclsecho.echo *********************学习-Bat-快速修改本地IP地址**********************echo.echo 实现功能(以下情况下测试通过):echo 1.多连接名称:自动获取网络连接名称echo 2.多连接属性:有线连接,无线连接,虚拟连接echo 3.多操作系统:Windows XP,Windows 7 / 8.1echo 4.多网络连接:无线连接2个,有线连接1个,VMware连接2个echo 5.区域的DNS:判断计算机可以连接互联网则采用当前DNS设置echo 6.空格名称:VMware Virtual Ethernet Adapter for VMnet1echo 7.优化设置:优化选择步骤,添加选择序号,保存配置内容echo 8.设置方法:可转换Netsh interface ip set 和WMIC nicconfig echo 9.获取优化:判断网关值,存储有效的网关值以打开路由器设置页echo.echo *********************学习-Bat-快速修改本地IP地址**********************ping -n 1 127.1 >nul:Set_localTempsetlocal enabledelayedexpansionver | 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_NetConnectionIDrem 获取设置网络连接名称clsecho.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=%%jEcho %%i_%%j>>适配器_Index_Name.txtecho. 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.txtREM 生成选择结构,添加序号ID_,避免纯查找数字序号会关键词的查找重复for /f "tokens=2-4 delims=_" %%a in (适配器_ID_Index_Name.txt) do (set NetConID=%%a set NetConnectionID_Index=%%bset NetConnectionName_Index=%%cecho 适配器ID:!NetConID! 标号Index:!NetConnectionID_Index! 名称:!NetConnectionName_Index!)echo *************选择网络连接名称*****************IN %WindowsVer%***********:Set_Net_AskEcho.echo.set /p NetConID=选择需要配置 适配器 对应 数字ID :if /i "%NetConID%" == "q" exitecho.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=%%cecho 当前选择适配器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_RamdonIPRem 如果计算机可以连接互联网,判断网络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+3set "net_IPnew=%IPa_%.%IPb_%"echo 随机IP前三位[%IPa_%] 生成随机IP [%net_IPnew%]ping 127.0.0.1 -n 1 >nul 1>nulping www.baidu.com &echo %errorlevel%if %errorlevel% equ 1 goto:Set_Net_Set_NoHaveif %errorlevel% equ 0 goto:Set_Net_Set_Have:Set_Net_Set_HaveEcho 当前计算机可以连通互联网...启用当前区域DNS...set Net_Ping=1set Net_IP_Diy=%net_IPnew%Rem 当前IP地址ping -n 1 127.0.0.1>nulgoto DiyCHOICE:Set_Net_Set_NoHaveEcho 当前计算机无法连通互联网...set Net_Ping=0set 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>nulgoto DiyCHOICE:DiyCHOICEclsecho.echo ****************************IP快速切换程序***************************echo.echo 当前选择网络连接:echo ID:%NetConID% 名称:%net_interface%echo.echo.echo --------------1.修改为随机IP---------------------%Net_IP_Diy%echo --------------2.设自动获取IP---------------------XXX.XXX.X.XXXecho --------------3.手动设网关IP---------------------192.XXX.X.XXXecho --------------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.:Diysetchoiceset /p SetChoice_C= 请输入操作:Echo.Echo.IF NOT "%SetChoice_C%"=="" SET SetChoice_C=%SetChoice_C:~0,1%if /i "%SetChoice_C%"=="1" goto Static_ipif /i "%SetChoice_C%"=="2" goto DHCP_ipif /i "%SetChoice_C%"=="3" goto Diy_ipif /i "%SetChoice_C%"=="4" goto Look_ipif /i "%SetChoice_C%"=="5" goto Set_Moden_Webif /i "%SetChoice_C%"=="r" goto rebootnetif /i "%SetChoice_C%"=="x" goto ENDecho 输入的选择超出范围...ping -n 3 127.1>nul goto DiyCHOICE:Static_ipecho.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=PRIMARYREM netsh interface ip add dns name="%net_interface%" addr=%Net_dns2% index=2wmic 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_ipecho. echo 正在设置IP为自动获取,请等待...REM echo 设置IP中...REM netsh interface ip set address name="%net_interface%" source=dhcpREM echo 设置网关成功...设置DNS中... REM netsh interface ip set dns "%net_interface%" source=dhcpwmic path Win32_NetworkAdapterConfiguration.index=%NetConnectionID_Index% call enabledhcpwmic path Win32_NetworkAdapterConfiguration.index=%NetConnectionID_Index% call SetDNSServerSearchOrder()goto Set_IP_Done:Diy_ipset/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_ipEcho.if "%LookNum%" == "2" (Echo 打开:控制面板-网络连接...ping -n 1 127.1>nulstart control ncpa.cplEcho 说明:开启环境延迟第二次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=1set /a LookNum+=1Echo 查看网络连接%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>nulgoto Set_IP_Done:rebootnetecho 重启网卡中...请稍等...ping -n 3 127.1>nulnetsh interface set interface "%net_interface%" disablednetsh interface set interface "%net_interface%" enablegoto Set_IP_Done:Set_Moden_WebEcho 需要修改%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_ipif /i "%SetChoice_Web%"=="2" goto Set_no_web_ipif /i "%SetChoice_Web%"=="2" goto DiyCHOICEecho 输入的选择超出范围...ping -n 3 127.1>nul goto :Set_Moden_Web:Set_web_ipRem 获得网关前三位,设置为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+3set "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_ipEcho 当前网关为:%Net_gateway% ,正在打开 http://%Net_gateway% ....ping %Net_gateway% >nulstart "%programfiles%\Internet Explorer\iexplore" http://%Net_gateway%ping -n 3 127.1>nulgoto :Set_IP_Done:Set_IP_Doneecho.echo OK,设置完成...ping -n 1 127.1>nul echo OK,任务完成,返回选择菜单...ping -n 1 127.1>nul GOTO DiyCHOICE:endecho **********************退出......................*********************Endlocalping -n 3 127.1>nulexit
继续分享一个
@echo offfor /f "delims=" %%a in ('wmic NIC where "NetEnabled='TRUE'" get MACAddress^,NetConnectionID /value^|find "="') do set %%afor /f "delims=" %%a in ('wmic NICCONFIG where "MACAddress='%MACAddress%'" get IPAddress /value^|find "="') do set %%afor /f "delims={," %%a in ("%IPAddress%") do set ip=%%~afor /f "tokens=1-4 delims=." %%a in ("%ip%") do set last=%%decho;%ip%echo;%last%echo;%NetConnectionID%netsh interface ipv4 set address name="本地连接" addr=192.168.1.%last% mask=255.255.255.0 gwmetric=30netsh interface ipv4 add address name="本地连接" addr=192.168.2.%last% mask=255.255.255.0 gateway=192.168.2.1 gwmetric=1exit
关于使用bat脚本文件怎么自动修改本机IP地址就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。