获取MAC网卡的地址和接口名称

Get MAC address and interface name of network card

我的工作站有几个网卡,它们通常有这样的默认接口名称:local area connectionlocal area connection 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

那么现在的问题就是如何实现第一步和第二步

非常感谢任何帮助。

  • 在英语系统语言的系统上使用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