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

时间:2015-07-17 11:47:14

标签: windows batch-file vbscript network-programming ip

我的工作站有几个网卡,通常有默认的接口名称,如下所示:本地连接本地连接2 等。

可能是网卡1被命名为本地连接3 ,网卡2被命名为本地连接等。

我正在尝试编写一个批处理(首选)或vbs脚本,它执行以下操作:

  1. 检查具有预定义列表的MAC地址的计算机中是否安装了特定网卡<​​/ li>
  2. 获取此网卡的接口名称
  3. 更改该网卡的接口名称
  4. 更改网卡配置(IP地址,子网掩码)
  5. 到目前为止,我发现我可以使用命令行更改接口名称:

    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步。

    非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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