【问题标题】:How can I find the MAC address of a specific device via Windows batch file?如何通过 Windows 批处理文件找到特定设备的 MAC 地址?
【发布时间】:2011-04-12 12:05:59
【问题描述】:

我正在尝试编写一个脚本,将本地连接的 MAC 地址与软件许可证中的 MAC 地址进行比较,以查看其中一个许可证是否与机器匹配。现在让我卡住的部分是提取特定设备“本地连接”的 MAC 地址。

我曾尝试使用以下搜索功能:

ipconfig /all | findstr^ /C:"Local Area Connection"^ /C:"Physical Address" > C:\temp\macaddress.txt
for /f "tokens=1,2 delims=:" %%i in (C:\temp\macaddress.txt) do @echo The MAC Address of %%i is %%j
pause

上述尝试中我确实不需要回显,但我使用它进行调试。

但上面的语句仍然将文本放入这样的文件中:

“物理地址…………:00-37-10-D1-98-2C

以太网适配器本地连接:

物理地址。 . . . . . . . . : 5D-26-0A-11-11-15" (我添加的引号以显示文本文件的开头和结尾)

因此,我不确定如何提取以太网适配器本地连接之后的 MAC 地址,尤其是当它们不在同一行时。

我需要在 Windows XP Professional 中使用批处理文件来执行此操作。谢谢。

【问题讨论】:

    标签: macos batch-file mac-address


    【解决方案1】:

    这个旧脚本应该可以工作。
    它首先搜索正确的适配器,然后等待直到包含字符串“Physical”的行。 :Normalize 函数用于在 XP 系统的 ipconfig 输出中删除 ,因为 microsoft 并不确切知道一行应该以 CR/LF 结尾而不是 LF/CR。

    @echo off
    SETLOCAL EnableDelayedExpansion EnableExtensions
    
    rem call :GetIP ip_WLAN "Drahtlos"
    rem echo ---
    set OS_Version=XP
    call :GetIP result  "Ethernet adapter" "Physical"
    
    echo mac=%result%
    
    goto :eof
    
    ::::::::::::::::::::::::::::
    :GetIP <resultVar> <AdapterName>
    :: resultVar    return variable for the searched value
    :: AdapterName  part of the adapter name
    setlocal
    set /a found=0
    if "%OS_Version%"=="Win7" set ipText=IPv4
    if "%OS_Version%"=="Vista" set ipText=IPv4
    if "%OS_Version%"=="XP" set ipText=IP-
    if "%~3"=="" (
        set searchText=!ipText!
    ) ELSE (
        set "searchText=%~3"
    )
    for /F "tokens=1,* delims=:" %%a IN ('ipconfig /all') DO (  
      call :Normalize first "%%a.dummy"
      call :Normalize post "%%b.dummy"
    
      if "!post!"=="_" (
        if "!first:%~2=!" NEQ "!first!" (
            set /a found=1
            rem echo adapter found "!first!"
        ) ELSE (
            if "!first!" NEQ "_" (
                set /a found=0
                rem echo - !first! !post!
            )
        )
      )
    
      if !found! EQU 1 (
        rem echo try "!first!"
        if "!first:%searchText%=!" NEQ "!first!" (
            set ipAddr=!post:_=!
            set ipAddr=!ipAddr: =!
            rem echo IP found !post! for adapter
        )
      )
    )
    
    (
      endlocal
      set %~1=%ipAddr%
      goto :eof
    )
    
    :Normalize
    set %~1=_%~n2
    goto :eof
    

    【讨论】:

    • 谢谢,@jeb!我不得不将评论指示符从“rem”更改为“REM!#”。除此之外,它看起来效果很好!太感谢了!我自己花了几天时间,但没有取得多大成功。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多