如何通过Windows批处理文件找到特定设备的MAC地址?
问题描述:
我正在尝试编写一个脚本,用于将本地连接的MAC地址与软件许可证中的MAC地址进行比较,以查看其中一个许可证是否与计算机匹配。现在让我卡住的部分是拉取特定设备“本地连接”的MAC地址。如何通过Windows批处理文件找到特定设备的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“ (引用我加入显示文本文件的开头和结尾)
因此,我不知道如何拉动以太网ada后的MAC地址本地连接,特别是当它们不在同一条线上时。
我需要在Windows XP Professional中使用批处理文件来做到这一点。谢谢。
答
这个旧脚本应该可以工作。
它首先搜索正确的适配器,然后等待包含字符串“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!#”。除此之外,它看起来很棒!非常感谢!我自己花了几天时间,但没有取得太大的成功。再次感谢! – KB3NRY 2011-04-12 12:41:30