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

发布于 2024-10-31 18:45:26 字数 659 浏览 5 评论 0原文

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

I'm trying to write a script that will compare the MAC address of Local Area Connection to the MAC address in software licenses to see if one of the licenses matches the machines. The part that has me stuck right now is pulling the MAC address of a specific device "Local Area Connection".

I have tried using search features such as:

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

I really don't need the echo in the above attempt, but I use it for debugging.

But still the above statement puts text into a file like this:

"Physical Address. . . . . . . . . : 00-37-10-D1-98-2C

Ethernet adapter Local Area Connection:

Physical Address. . . . . . . . . : 5D-26-0A-11-11-15"
(quotes added by me to show the beginning and end of text file)

From that, I'm not sure how to pull the MAC address that comes after Ethernet adapter Local Area Connection, especially when they're not on the same line.

I need to do this with a batch file in Windows XP Professional. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-11-07 18:45:26

这个旧脚本应该可以工作。
它首先搜索正确的适配器,然后等待包含字符串“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

This old script should work.
It searches first for the correct adapter, and then it waits until a line containing the string "Physical".
The :Normalize function is for removing < Carriage Return> in the output of ipconfig at XP systems, as microsoft doesn't know exactly that a line should end with CR/LF not with 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文