自動修改本機IP地址的bat程式碼
阿新 • • 發佈:2020-01-09
程式碼比較多,經過我們小編測試,在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^,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、子網掩碼、閘道器設定好,執行執行就可以了,方便粗暴。