【问题标题】:Get MAC address and interface name of network card获取网卡的MAC地址和接口名称
【发布时间】:2015-10-07 03:12:05
【问题描述】:

我的工作站有几块网卡,它们的默认接口名称通常是这样的:本地连接本地连接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步。

非常感谢任何帮助。

【问题讨论】:

  • 如果您只想为您的工作站执行此操作,为什么不打开控制面板-网络和共享中心点击左侧更改适配器设置,通过上下文菜单重命名网络适配器并通过上下文菜单项属性更改 IPv4 和/或 IPv6 设置?
  • Right ;) 不,我想在我自己的工作站上运行它后立即在许多其他工作站上执行此操作。

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


【解决方案1】:
  • 在具有英语系统语言的系统上使用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
    

【讨论】:

  • 嗨!非常感谢!这完美!我将解决方案与 getmac 一起使用。是否可以检查 MAC 地址是否在有效 MAC 地址列表中,而不是检查一个特定地址(无需过多接触代码)?
  • @Rickson1982,您如何定义/输入有效 MAC 列表?
  • @wOxxOm:嗨,我想到了。像这样:设置列表=“01-02-03-04-05-06 AA-BB-CC-DD-EE-FF”等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
相关资源
最近更新 更多