iT邦幫忙

0

關於批次檔設定固定IP

ryango 2017-06-29 22:50:4010967 瀏覽

目前小弟在學習批次檔的練習階段遇到一個問題,想請教各位批次檔的高手,就是小弟在寫批次檔設定固定IP時,使用:
netsh interface ip set address Ethernet0 static 192.168.0.1 255.255.255.0

但碰到一個問題,就是一但 網域名稱不是Ethernet0,要是換了平台變成Ethernet,就會無法設定IP。
我目前用的方法是 在批次檔內加入很多行:
netsh interface ip set address Ethernet0 static 192.168.0.1 255.255.255.0
然後把Ethernet設成0~10。這樣才能在遇到不同的 網域名稱時也可以設定固定IP。

但我想請問各位高手,有沒有更好的辦法可以讓批次檔直接找到網域名稱呢?謝謝。

看更多先前的討論...收起先前的討論...
James iT邦大師 6 級 ‧ 2017-06-30 08:45:59 檢舉
批次檔易學難用,如果要進階一點的功能還是要用wscript或powershell,
google也很多現成範例。
ryango iT邦新手 5 級 ‧ 2017-06-30 19:49:23 檢舉
LKK您好,感謝您的意見我會朝這個方向努力的,謝謝您。
eigen iT邦新手 1 級 ‧ 2017-06-30 23:05:48 檢舉
先找幾台 ethernet? 不一樣的電腦,執行 ipconfig >1.txt

將1.txt 貼上來看一下。
ryango iT邦新手 5 級 ‧ 2017-07-01 15:33:22 檢舉
eigen您好,
Windows IP Configuration


Ethernet adapter Ethernet0:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::3dd8:480d:4f83:2a23%4
IPv4 Address. . . . . . . . . . . : 192.168.0.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
jeles51
iT邦研究生 3 級 ‧ 2017-06-30 08:41:15
@echo off
REM get ethernet name to eth.txt
wmic.exe nic where "NetConnectionStatus=2" get NetConnectionID |more +1 > eth.txt
REM 設為變數 from eth.txt
set /p eth=<eth.txt
REM Set Fixed IP
netsh interface ipv4 set address name=%eth% static 1.1.1.100 255.255.255.0 1.1.1.254
REM Set DNS
REM Primary DNS=8.8.8.8 / Secondary DNS = 8.8.4.4
netsh interface ipv4 set dns name=%eth% static 8.8.8.8
netsh interface ipv4 add dns name=%eth% 8.8.4.4 index=2

參考:
http://ithelp.ithome.com.tw/questions/10185225
and
Google "get Local Area Connection name"
Google "How to read file contents into a variable"

ryango iT邦新手 5 級 ‧ 2017-06-30 19:48:45 檢舉

喬可您好,感謝您的解答,但我在使用這段bat碼時,發生了一個問題,那就是 執行時出現了:
Invalid source parameter <3>.
我有進去wmic看了一下

是否與這行的configManagerUserconfig顯示FALSE有關呢?

ryango iT邦新手 5 級 ‧ 2017-06-30 20:57:19 檢舉

請問是否更改了path設定會導致這串指令無法執行呢?謝謝您

ryango iT邦新手 5 級 ‧ 2017-07-01 23:29:08 檢舉

喬可您好,可以請教您一下 您寫的這段:
get NetConnectionID |more +1
是什麼意思嗎? 謝謝您。

我要發表回答

立即登入回答