iT邦幫忙

0

請問批次檔案改IP的區域連線問題 新手初次發問

  • 分享至 

  • xImage

各位高手好,小弟初次來發問 有關於改IP的批次檔

netsh interface ipv4 set address "區域連線" static 192.168.1.38 255.255.255.0 192.168.1.1

netsh interface ipv4 set dnsserver "區域連線" static 8.8.8.8 primary

netsh interface ipv4 add dns "區域連線" 8.8.4.4

想弄一個一鍵更改IP的批次檔,但有時候是 "區域連線 1" "區域連線2" "區域連線 3"

請問如果找不到"區域連線"的話,有辦法可以判斷接著找其他的嗎?

目前區域連線0~8都各寫三行,但感覺有點笨,想請教有沒有比較好的方法呢

vit5015 iT邦新手 3 級 ‧ 2018-10-12 16:41:15 檢舉
https://ithelp.ithome.com.tw/questions/10178617
二樓的回答供你參考
ayu iT邦好手 2 級 ‧ 2018-10-12 23:54:31 檢舉
多個 區域連線 的快速切換嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
蟹老闆
iT邦大師 1 級 ‧ 2018-10-13 16:01:16
最佳解答

請參考

@Echo Off
Md "%windir%\MdTest" 2>nul&&Rd "%windir%\MdTest" >nul 2>nul||(Echo.&Echo.&Echo.	 動作完未成!&Echo 請用右鍵以系統管理員身分執行這個程式&Echo.&Echo.&Echo.&Echo.&Pause&Exit)
Set IP=192.168.1.38
Set GW=192.168.1.1
Set Mask=255.255.255.0
Set DNSprimary=8.8.8.8
Set DNSsecondary=8.8.4.4
For /f "skip=2 tokens=2-3 delims=," %%i In ('Wmic NIC Where ^(PhysicalAdapter^=True and NetConnectionStatus^=2^) Get NetConnectionID^,MACAddress^,netconnectionstatus /Format:CSV') Do (
	netsh interface ipv4 set dnsserver "%%j" static %DNSprimary% primary
	netsh interface ipv4 add dnsserver "%%j" %DNSsecondary%
	netsh interface ipv4 set address "%%j" static %IP% %Mask% %GW% 1
	Goto :OK
)
Echo.		找不到可用的網路卡介面.
Timeout /t 3
Exit /b
:OK
Echo.		設定完成.
Timeout /t 3
Exit /b

說明:
若有多個網卡,只抓取有接線的第一個網卡進行設定,其餘網卡略過不處理。
若第一個網卡己設為手動設定則略過不進行處理(錯誤訊息)。

0
comhlp
iT邦新手 4 級 ‧ 2018-11-07 12:01:07

用POWERSHELL 簡短一點

 $nic=Get-NetAdapter -Name "*" -physical|sort-Object Name |select -ExpandProperty Name -last 1
 $IP="192.168.1.38"
 $GW="192.168.1.1"
 $MASK="255.255.255.0"
 $DNS1="8.8.8.8"
 $DNS2="8.8.8.4"
netsh interface ipv4 set dnsserver "$nic" static $DNS1 primary
netsh interface ipv4 add dnsserver "$nic" $DNS2
netsh interface ipv4 set address "$nic" static $IP $MASK $GW 1

我要發表回答

立即登入回答