主頁(yè) > 知識(shí)庫(kù) > 自動(dòng)修改本機(jī)IP地址的bat代碼

自動(dòng)修改本機(jī)IP地址的bat代碼

熱門標(biāo)簽:電話機(jī)器人全國(guó)招商 雷霆電銷機(jī)器人怎么樣 呼倫貝爾外呼系統(tǒng) 玉林市機(jī)器人外呼系統(tǒng)哪家好 電話機(jī)器人產(chǎn)品怎么樣 清遠(yuǎn)百度地圖標(biāo)注店鋪位置 智能打電話機(jī)器人收費(fèi) 如何申請(qǐng)400電話業(yè)務(wù)mm 如何弄地圖標(biāo)注

代碼比較多,經(jīng)過(guò)腳本之家小編測(cè)試,在2008 r2上無(wú)法正常執(zhí)行,其實(shí)bat設(shè)置ip就是幾個(gè)命令,無(wú)外乎加了一些判斷與獲取原來(lái)網(wǎng)絡(luò)配置的一些信息,代碼越復(fù)雜越容易出問(wèn)題

@echo off
setlocal enabledelayedexpansion
title 修改本機(jī)IP地址 by:小小滄海20130409
:init
clsecho ——————————————自動(dòng)修改本機(jī)IP地址——————————————
rem 系統(tǒng)版本,值可為Windows7或是WindowsXP,或是auto(表示自動(dòng)獲?。?
set SYSVER=auto
rem 要更改的網(wǎng)卡名稱,auto表示自動(dòng)獲取第一塊“以太網(wǎng)適配器”
set ETH=auto
rem IP來(lái)源,值僅為兩個(gè)static和dhcp,ques表示詢問(wèn),由使用者填寫
rem 靜態(tài)IP請(qǐng)?zhí)顚憇tatic,從網(wǎng)關(guān)自動(dòng)獲取IP請(qǐng)?zhí)顚慸hcp
set IPSOURCE=ques
rem 要改成的IP地址,ques同上
set IPADDR=ques
rem 要改成的子網(wǎng)掩碼,ques同上
set MASK=ques
rem 要使用的默認(rèn)網(wǎng)關(guān),ques同上
set GATEWAY=ques
rem DNS模式,值僅為兩個(gè)static和dhcp
rem 靜態(tài)DNS請(qǐng)?zhí)顚憇tatic,從網(wǎng)關(guān)自動(dòng)獲取DNS請(qǐng)?zhí)顚慸hcp
set DNSSOURCE=ques
rem 要使用的首選DNS,ques同上
set DNS1=ques
rem 要使用的備用DNS,ques同上
set DNS2=ques
set LOG=%TEMP%\changeIP_log.txt
echo 運(yùn)行日期:%date% %time%>%LOG%


:start
rem ===============使用者填寫參數(shù)值=======================
rem 自動(dòng)獲取系統(tǒng)版本,結(jié)果為 Windows7 或是 WindowsXP(只測(cè)試了這兩個(gè)系統(tǒng))
if "%SYSVER%"=="auto" (
  set /p=正在自動(dòng)獲取系統(tǒng)版本...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系統(tǒng)不保證能執(zhí)行成功!pause>nul
    )
  )
)

rem 自動(dòng)獲取網(wǎng)卡名稱
if "%ETH%"=="auto" (
  echo 正在自動(dòng)獲取網(wǎng)絡(luò)適配器信息...
  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=請(qǐng)選擇要設(shè)置的網(wǎng)卡編號(hào):nul
    set select=0set /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 自動(dòng)獲取網(wǎng)卡名稱失敗,請(qǐng)右鍵編輯本批處理,手動(dòng)填寫網(wǎng)卡名稱!pause>nulexit
  ) else (
    rem set/p=[!ETH!]nul
    echo 成功!
  )
)

:quesIP
if "%IPSOURCE%"=="ques" (
  echo →請(qǐng)?zhí)顚憽綢P地址來(lái)源】^(值僅為兩個(gè)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來(lái)源填寫錯(cuò)誤,將變更為static模式
    pause>nul
  ))
)
if /i "%IPSOURCE%"=="dhcp" goto quesDNS

if "%IPADDR%"=="ques" (
  echo →請(qǐng)?zhí)顚憽綢P地址】^(直接回車為192.168.1.100^)
  set /p IPADDR=
  if "!IPADDR!"=="ques" set IPADDR=192.168.1.100
)

if "%MASK%"=="ques" (
  echo →請(qǐng)?zhí)顚憽咀泳W(wǎng)掩碼】^(直接回車為255.255.255.0^)
  set /p MASK=
  if "!MASK!"=="ques" set MASK=255.255.255.0
)

if "%GATEWAY%"=="ques" (
  echo →請(qǐng)?zhí)顚憽灸J(rèn)網(wǎng)關(guān)】^(直接回車為192.168.1.1^)
  set /p GATEWAY=
  if "!GATEWAY!"=="ques" set GATEWAY=192.168.1.1
)

:quesDNS
if "%DNSSOURCE%"=="ques" (
  echo →請(qǐng)?zhí)顚憽綝NS來(lái)源】^(值僅為兩個(gè)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來(lái)源填寫錯(cuò)誤,將變更為static模式
    pause>nul
  ))
)
if /i "%DNSSOURCE%"=="dhcp" goto checkInfo

if "%DNS1%"=="ques" (
  echo →請(qǐng)?zhí)顚憽臼走xDNS地址】^(直接回車為8.8.8.8^)
  set /p DNS1=
  if "!DNS1!"=="ques" set DNS1=8.8.8.8
)

if "%DNS2%"=="ques" (
  echo →請(qǐng)?zhí)顚憽緜溆肈NS地址】^(直接回車為8.8.4.4^)
  set /p DNS2=
  if "!DNS2!"=="ques" set DNS2=8.8.4.4
)


:checkInfo
cls
echo 即將應(yīng)用以下配置:
call :showInfo
echo 請(qǐng)確認(rèn)信息是否正確,輸入Y繼續(xù),輸入N退出,輸入Q顯示本機(jī)網(wǎng)絡(luò)信息
set choose=nulset /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 ★注意★請(qǐng)關(guān)閉防火墻或允許所有彈出的安全軟件提示,否則無(wú)法成功執(zhí)行!

:changeIP
rem 通過(guò)dhcp刪除原有IP配置
echo →設(shè)置"%ETH%"的IP源為DHCP,以刪除原有IP地址 >>%LOG%
netsh -c interface ip set address name="%ETH%" source=dhcp >>%LOG%
if /i "%IPSOURCE%"=="static" (
  echo →設(shè)置IP為"%IPADDR%",掩碼為"%MASK%",網(wǎng)關(guān)為"%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 →設(shè)置首選DNS為%DNS1% >>%LOG%
  netsh -c interface ip add dns name="%ETH%" addr="%DNS1%" index=1 >>%LOG%
  echo →設(shè)置備用DNS為%DNS2% >>%LOG%
  netsh -c interface ip add dns name="%ETH%" addr="%DNS2%" index=2 >>%LOG%
  rem ↑此處可繼續(xù)增加多個(gè)DNS服務(wù)器地址
) else (if /i "%DNSSOURCE%"=="dhcp" (
  echo →設(shè)置DNS為DHCP模式 >>%LOG%
  netsh -c interface ip set dns name="%ETH%" dhcp >>%LOG%
))

:end
cls
rem echo 【要設(shè)定的信息】
rem call :showInfo
echo 【當(dāng)前本機(jī)信息】
call :getInfo
echo ======================================
echo 如果上下一致則說(shuō)明修改成功!
echo 如果不一致則請(qǐng)查看日志文件!
echo 輸入L查看日志文件,輸入E退出程序。
set choose=nulset /p choose=
if /i "%choose%"=="L" start %LOG%goto end
if /i "%choose%"=="E" exit
if /i "%choose%"=="nul" goto end


echo 程序執(zhí)行結(jié)束,按任意鍵退出...
pause>nul
exit


:showInfo
echo 【本機(jī)系統(tǒng)】:%SYSVER%
echo 【網(wǎng)卡名稱】:%ETH%
echo 【IP來(lái)源 】:%IPSOURCE%
if "%IPSOURCE%"=="static" (
  echo 【IP地址 】:%IPADDR%
  echo 【子網(wǎng)掩碼】:%MASK%
  echo 【默認(rèn)網(wǎng)關(guān)】:%GATEWAY%
)
echo 【DNS來(lái)源 】:%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
  子網(wǎng)前綴:             192.168.1.0/24 (掩碼 255.255.255.0)
  默認(rèn)網(wǎng)關(guān):             192.168.1.1
  網(wǎng)關(guān)躍點(diǎn)數(shù):            1
  InterfaceMetric:          20
  
  
接口 "本地連接" 的配置
  靜態(tài)配置的 DNS 服務(wù)器:      8.8.8.8
                   8.8.4.4
  用哪個(gè)前綴注冊(cè):          只是主要

BAT批處理復(fù)雜環(huán)境下的IP地址修改設(shè)置

@echo off
mode con cols=70 lines=38
color a
title D-小蘇-學(xué)習(xí)-Bat-快速修改本地IP地址
:ShowReadme
cls
echo.
echo *********************學(xué)習(xí)-Bat-快速修改本地IP地址**********************
echo.
echo 實(shí)現(xiàn)功能(以下情況下測(cè)試通過(guò)):
echo 1.多連接名稱:自動(dòng)獲取網(wǎng)絡(luò)連接名稱
echo 2.多連接屬性:有線連接,無(wú)線連接,虛擬連接
echo 3.多操作系統(tǒng):Windows XP,Windows 7 / 8.1
echo 4.多網(wǎng)絡(luò)連接:無(wú)線連接2個(gè),有線連接1個(gè),VMware連接2個(gè)
echo 5.區(qū)域的DNS:判斷計(jì)算機(jī)可以連接互聯(lián)網(wǎng)則采用當(dāng)前DNS設(shè)置
echo 6.空格名稱:VMware Virtual Ethernet Adapter for VMnet1
echo 7.優(yōu)化設(shè)置:優(yōu)化選擇步驟,添加選擇序號(hào),保存配置內(nèi)容
echo 8.設(shè)置方法:可轉(zhuǎn)換Netsh interface ip set 和WMIC nicconfig 
echo 9.獲取優(yōu)化:判斷網(wǎng)關(guān)值,存儲(chǔ)有效的網(wǎng)關(guān)值以打開(kāi)路由器設(shè)置頁(yè)
echo.
echo *********************學(xué)習(xí)-Bat-快速修改本地IP地址**********************
ping -n 1 127.1 >nul
:Set_localTemp
setlocal enabledelayedexpansion
ver | find "6." > NUL (clsecho. echo -------------------------------當(dāng)前系統(tǒng)為 Windows 7以上系統(tǒng) Set "WindowsVer=Windows 7" goto :Set_NetConnectionID)
clsecho. echo -------------------------------當(dāng)前系統(tǒng)不是 Windows 7以上系統(tǒng) Set "WindowsVer=WindowsXP" goto :Set_NetConnectionID
:Set_NetConnectionID
rem 獲取設(shè)置網(wǎng)絡(luò)連接名稱
cls
echo.
echo *************選擇網(wǎng)絡(luò)連接名稱*****************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_模式 : 自動(dòng)
    ) else echo 適配器_IP_模式 : 手動(dòng) 
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 適配器_子網(wǎng)掩碼: !IPSubnet!  Net_子網(wǎng)掩碼: !Net_MASK!
IF not defined Net_gateway set Net_gateway=!DefaultIPGateway!
    echo 適配器_默認(rèn)網(wǎng)關(guān): !DefaultIPGateway!  Net_默認(rèn)網(wǎng)關(guān): !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 ************************選擇網(wǎng)絡(luò)連接名稱*****************************
(for /f "tokens=1* delims=:" %%a in ('findstr /n .* 適配器_Index_Name.txt') do echo ID_%%a_%%b)>適配器_ID_Index_Name.txt
REM 生成選擇結(jié)構(gòu),添加序號(hào)ID_,避免純查找數(shù)字序號(hào)會(huì)關(guān)鍵詞的查找重復(fù)
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! 標(biāo)號(hào)Index:!NetConnectionID_Index! 名稱:!NetConnectionName_Index!
)
echo *************選擇網(wǎng)絡(luò)連接名稱*****************IN %WindowsVer%***********
:Set_Net_Ask
Echo.
echo.
set /p NetConID=選擇需要配置 適配器 對(duì)應(yīng) 數(shù)字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 當(dāng)前選擇適配器ID:!NetConID! 標(biāo)號(hào)Index:!NetConnectionID_Index! 名稱:!NetConnectionName_Index!
)
if "%NetConnectionName_Index%" == "" (echo 選擇選項(xiàng)超出范疇 ... 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 如果計(jì)算機(jī)可以連接互聯(lián)網(wǎng),判斷網(wǎng)絡(luò)DNS、網(wǎng)關(guān)是否可用,可以連接互聯(lián)網(wǎng)則只改IP,啟用當(dāng)前區(qū)域DNS...
Rem 獲得可上網(wǎng)IP前三位,設(shè)置為隨機(jī)IP前三位
if "%Net_IP%" == "" (echo 設(shè)置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 隨機(jī)IP前三位[%IPa_%]  生成隨機(jī)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 當(dāng)前計(jì)算機(jī)可以連通互聯(lián)網(wǎng)...啟用當(dāng)前區(qū)域DNS...
set Net_Ping=1
set Net_IP_Diy=%net_IPnew%
Rem 當(dāng)前IP地址
ping -n 1 127.0.0.1>nul
goto DiyCHOICE
:Set_Net_Set_NoHave
Echo 當(dāng)前計(jì)算機(jī)無(wú)法連通互聯(lián)網(wǎng)...
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 當(dāng)前選擇網(wǎng)絡(luò)連接:
echo  ID:%NetConID%  名稱:%net_interface%
echo.
echo.
echo --------------1.修改為隨機(jī)IP---------------------%Net_IP_Diy%
echo --------------2.設(shè)自動(dòng)獲取IP---------------------XXX.XXX.X.XXX
echo --------------3.手動(dòng)設(shè)網(wǎng)關(guān)IP---------------------192.XXX.X.XXX
echo --------------4.查看網(wǎng)絡(luò)設(shè)置---------------------%Net_IP%
echo --------------5.設(shè)置路由器頁(yè)---------------------%Net_gateway%
echo.
echo --------------r.重啟網(wǎng)卡-------------------------x.退出程序---
echo.
echo.
echo **** IN %WindowsVer% *******************************Tools By S34205****
echo.
echo.
echo.
if "%Net_Ping%" == "1" (echo 當(dāng)前計(jì)算機(jī)可以連接互聯(lián)網(wǎng),啟用當(dāng)前區(qū)域DNS...) else (echo 當(dāng)前計(jì)算機(jī)無(wú)法連接互聯(lián)網(wǎng),可能需要設(shè)置您區(qū)域DNS....)
echo 隨機(jī)的IP:[%Net_IP_Diy%]
echo 子網(wǎng)掩碼:[%Net_MASK%]
echo 默認(rèn)網(wǎng)關(guān):[%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= 請(qǐng)輸入操作:
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 設(shè)置隨機(jī)IP:[%Net_IP_Diy%] ...
REM netsh interface ip set address name="%net_interface%" source=static addr=%Net_IP_Diy% mask=%net_MASK%
REM echo 設(shè)置IP成功...
REM Echo 設(shè)置網(wǎng)關(guān)... 
REM netsh interface ip set address name="%net_interface%" gateway=%Net_gateway% gwmetric=1 
REM echo 設(shè)置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 正在設(shè)置IP為自動(dòng)獲取,請(qǐng)等待...
REM echo 設(shè)置IP中...
REM netsh interface ip set address name="%net_interface%" source=dhcp
REM echo 設(shè)置網(wǎng)關(guān)成功...設(shè)置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=請(qǐng)輸入IP地址 XXX.XXX.XXX.XXX:
set/p Net_gateway=請(qǐng)輸入網(wǎng)關(guān)地址 XXX.XXX.XXX.XXX: 
set/p Net_dnsPra=請(qǐng)輸入主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 打開(kāi):控制面板-網(wǎng)絡(luò)連接...
ping -n 1 127.1>nul
start control ncpa.cpl
Echo 說(shuō)明:開(kāi)啟環(huán)境延遲第二次Bat運(yùn)行適配器屬性不會(huì)改變,所以第二次次信息查看需要手動(dòng)查看屬性... Echo.
echo,前次%net_interface%地址的IP:%IPAddress% echo,前次%net_interface%子網(wǎng)掩碼:%IPSubnet% echo,前次%net_interface%默認(rèn)網(wǎng)關(guān):%DefaultIPGateway% 
ping -n 6 127.1>nul goto Set_IP_Done
)
set LookNum=1
set /a LookNum+=1
Echo 查看網(wǎng)絡(luò)連接%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%子網(wǎng)掩碼:%IPSubnet%
echo,%net_interface%默認(rèn)網(wǎng)關(guān):%DefaultIPGateway%
ping -n 6 127.1>nul
goto Set_IP_Done
:rebootnet
echo 重啟網(wǎng)卡中...請(qǐng)稍等...
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為網(wǎng)關(guān)地址段[%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 獲得網(wǎng)關(guān)前三位,設(shè)置為IP前三位,如果不存在則創(chuàng)建
if "%Net_gateway%" == "" (echo 設(shè)置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 正在設(shè)置%net_interface%為網(wǎng)關(guān)IP[%Set_web_net_IPnew%]......
wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Set_web_net_IPnew%),(%net_MASK%)
echo 已經(jīng)設(shè)置%net_interface%為網(wǎng)關(guān)IP[%Set_web_net_IPnew%]......
goto :Set_no_web_ip
:Set_no_web_ip
Echo 當(dāng)前網(wǎng)關(guān)為:%Net_gateway% ,正在打開(kāi) 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,設(shè)置完成...
ping -n 1 127.1>nul 
echo OK,任務(wù)完成,返回選擇菜單...
ping -n 1 127.1>nul 
GOTO DiyCHOICE
:end
echo **********************退出......................*********************
Endlocal
ping -n 3 127.1>nul
exit

繼續(xù)分享一個(gè)

@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、子網(wǎng)掩碼、網(wǎng)關(guān)設(shè)置好,執(zhí)行運(yùn)行就可以了,方便粗暴。

您可能感興趣的文章:
  • 批處理bat設(shè)置固定IP地址和自動(dòng)IP地址
  • bat腳本顯示本機(jī)IP地址的兩種方法(內(nèi)網(wǎng)ip)
  • BAT批處理判斷IP地址并自動(dòng)禁用啟用網(wǎng)卡
  • 一鍵自動(dòng)更改本機(jī)IP地址BAT執(zhí)行腳本 非常好用
  • bat 選項(xiàng)之修改ip的腳本代碼(讓用戶可以選擇操作并進(jìn)行判斷執(zhí)行)
  • IP動(dòng)態(tài)切換bat腳本
  • 批處理獲取IP生成TXT文本的bat代碼
  • 顯示自己的IP的批處理bat文件
  • 用bat文件修改ip

標(biāo)簽:江西 株洲 臺(tái)州 樂(lè)山 三門峽 蕪湖 公主嶺 白銀

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《自動(dòng)修改本機(jī)IP地址的bat代碼》,本文關(guān)鍵詞  自動(dòng),修改,本機(jī),地址,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《自動(dòng)修改本機(jī)IP地址的bat代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于自動(dòng)修改本機(jī)IP地址的bat代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章