当前位置: 移动技术网 > IT编程>网络>Dos/Bat > 自动修改本机IP地址的bat代码

自动修改本机IP地址的bat代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

代码比较多,经过移动技术网小编测试,在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、子网掩码、网关设置好,执行运行就可以了,方便粗暴。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网