如何通过Windows批处理文件找到特定设备的MAC地址?

时间:2011-04-12 12:05:59

标签: macos batch-file mac-address

我正在尝试编写一个脚本,将本地连接的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中使用批处理文件执行此操作。谢谢。

1 个答案:

答案 0 :(得分:0)

这个旧脚本应该有效 它首先搜索正确的适配器,然后等待直到包含字符串“Physical”的行。 :Normalize函数用于删除<在XP系统的ipconfig输出中回车> ,因为微软并不确切地知道一条线应该以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