簡體   English   中英

獲取網卡的MAC地址和接口名稱

[英]Get MAC address and interface name of network card

我的工作站有幾個網卡,這些網卡通常具有默認的接口名稱,例如: 本地連接本地連接2等。

可能是網卡1被稱為本地連接3 ,網卡2被稱為本地連接等。

我正在嘗試編寫執行以下操作的批處理(首選)或vbs腳本:

  1. 檢查機器中是否安裝了具有預定義列表的MAC地址之一的特定網卡
  2. 獲取此網卡的接口名稱
  3. 更改該網卡的接口名稱
  4. 更改網卡的配置(IP地址,子網掩碼)

到目前為止,我發現可以使用命令行更改接口名稱:

netsh interface set interface name="local area connection" newname="newNetworkName"

而且我可以使用以下方式更改配置:

netsh int ip set address "local area connection" static 192.168.0.101 255.255.255.0 0.0.0.0

因此,現在的問題是如何實現步驟1和步驟2。

任何幫助都非常感謝。

  • 在具有英語系統語言的系統上使用ipconfig /all

     @echo off setlocal enableDelayedExpansion set MACLIST=01-02-03-04-05-06 AA-BB-CC-DD-EE-FF for /f "delims=" %%a in ('ipconfig /all') do ( set line=%%a if not "!line:~0,1!"==" " if not "!line:adapter=!"=="!line!" ( set name=!line:*adapter =! set name=!name::=! ) for /f "tokens=1,2,*" %%b in ("%%a") do ( if "%%b %%c"=="Physical Address." ( set mac=%%d set mac=!mac:*: =! echo !name!: !mac! call set mactest=%%MACLIST:!mac!=%% if not "!MACLIST!"=="!mactest!" ( netsh interface set interface name="!name!" newname="newNetworkName1" netsh int ip set address "newNetworkName1" static 192.168.0.101 255.255.255.0 0.0.0.0 ) ) ) ) pause 
  • 與語言無關,使用getmac (盡管需要1秒鍾才能執行):

     @echo off setlocal enableDelayedExpansion set MACLIST=01-02-03-04-05-06 AA-BB-CC-DD-EE-FF for /f "delims=" %%a in ('getmac /fo csv /nh /v') do ( set line=%%a&set line=!line:"=,! for /f "delims=,,, tokens=1,3" %%b in ("!line!") do ( set name=%%b set mac=%%c call set mactest=%%MACLIST:!mac!=%% if not "!MACLIST!"=="!mactest!" ( netsh interface set interface name="!name!" newname="newNetworkName1" netsh int ip set address "newNetworkName1" static 192.168.0.101 255.255.255.0 0.0.0.0 ) ) ) pause 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM