ipconfig /all|find "Physical Address">c:\windows\temp\macaddress.txt
for /f "tokens=2 delims=:" %%i in (c:\windows\temp\macaddress.txt) do
@echo The MAC Address is %%i
The problem is that under Vista, I end up with three MAC addresses
displayed because there are multiple Physical Addresses listed in
IPCONFIG's output (listed below).
How can I limit the display to the Ethernet adapter's MAC address?
IPCONFIG /ALL
Windows IP Configuration
Host Name . . . . . . . . . . . . : XXXXXX
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : mycompany.com
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : mycompany.com
Description . . . . . . . . . . . : Intel(R) 82566DM-2 Gigabit
Network Connection
Physical Address. . . . . . . . . : 00-1A-A0-C3-3C-5D
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . :
fe80::f03c:d8f1:d28b:befc%8(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.0.111(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, January 21, 1872 8:57:34
AM
Lease Expires . . . . . . . . . . : Sunday, March 02, 2008 12:25:49
PM
Default Gateway . . . . . . . . . : 192.168.0.1
DHCP Server . . . . . . . . . . . : 192.168.0.1
DNS Servers . . . . . . . . . . . : 192.168.0.1
192.168.0.2
Primary WINS Server . . . . . . . : 192.168.0.1
Secondary WINS Server . . . . . . : 192.168.0.2
NetBIOS over Tcpip. . . . . . . . : Enabled
Tunnel adapter Local Area Connection* 6:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : mycompany.com
Description . . . . . . . . . . . : isatap.mycompany.com
Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Tunnel adapter Local Area Connection* 7:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
Physical Address. . . . . . . . . : 02-00-54-55-4E-01
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Regards,
Dave
With something like the following.
@REM locate MAC ID in ipconfig/all output
@setlocal enableextensions
@echo off
set f=%0.tmp
ipconfig/all | findstr ^
/C:"Ethernet adapter Local Area Connection:" ^
/C:"Physical Address" ^
> %f%
for /F "delims=" %%a in (%f%) do call :PROC "%%a"
echo The MAC Address is: %MACA%
goto :EOF
:PROC
set a=%~1
set a=%a: =%
set a=%a:.=%
if "%a:~0,8%" == "Ethernet" (
set s=next line will have MACA
) else if defined s (
set MACA=%a:PhysicalAddress:=%
set s=
)
Note: findstr.exe is a standard utility included with Windows XP. If
you have find.exe, you should also have findstr.exe.
Forgot to add the statement
del %f%
between the two statements above.
Here's a one-liner using "getmac" instead of "ipconfig":
for /f "tokens=3 delims=," %a in ('"getmac /v /fo csv | findstr
Ethernet"') do echo %a
--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
a>
Assuming that the address that you want is the first "Physical Address" (you
didn't say)
[1]SET MAC=
[2]ipconfig /all|find "Physical Address">c:\windows\temp\macaddress.txt
[3]for /f "tokens=2 delims=:" %%i in (c:\windows\temp\macaddress.txt) do IF
NOT DEFINED MAC SET MAC=%%i&echo The MAC Address is %%i
Three lines - each prefixed by [number] for clarity.
OR
[1]SET MAC=
[2]for /f "tokens=2 delims=:" %%i in ( ' ipconfig /all|find "Physical
Address" ' ) do IF NOT DEFINED MAC SET MAC=%%i&echo The MAC Address is %%i
where the single-quotes are required but the spaces surrounding them are
not - they're just there for clarity.
Note that AFTER the last line of these snippets, the MAC address is
available as %MAC%, there is NO spaces EITHER side of the "=" in the SET
statements.
See the FOR documentation for use of the ... in ( ' ... ' ) do ... syntax
for /?
from the prompt
or alt.msdos.batch.nt - where such matters are regularly discussed.
Your approach works for the OP's sample ipconfig output, but it's not
general. Your approach assigns the first physical address in the text
stream to MAC, but the active Ethernet adapter need not be the first
section with a physical address. On my own system, my dial-up VPN
section comes before my regular (Ethernet adapter Local Area
Connection) section.
Anyway, the getmac solution would be the ideal way to go. Learn
something new every day.
Thanks, but I'm not certain that the Ethernet Adapter will always be the
first physical address, which is why I specified I wanted the MAC
address of the Ethernet adapter.
As to alt.msdos.batch.nt, I would normally have posted there but the
news provider I have available to me at work is down, so for now I'm
limited to the public Microsoft groups.
Regards,
Dave
Thanks for the confirmation, I suspected there might be a problem if I
just grabbed the 1st physical address. I'm off to look at the getmac
solution!
Regards,
Dave
Thanks for the help, Harlan. Ultimately, I think I'll go with the
getmac solution below.
Regards,
Dave
Thanks for the help! I didn't know about getmac, I'll have to remember
it.
On my sample Vista machine, the output from getmac /v /fo csv was as
follows:
"Connection Name","Network Adapter","Physical Address","Transport Name"
"Local Area Connection","Intel(R) 82566DM-2 Gigabit Network
Connection","00-1A-A0-C3-3C-5D","\Device\Tcpip_{5EEE37DE-FFF9-445E-B3EC-060853AD5897}"
Interesting that it only lists one adapter as opposed to ipconfig
listing 3. Also since Ethernet doesn't appear in the output, findstr
Ethernet wasn't working, but easily remedied.
Here's what I ended up with:
for /f "tokens=3 delims=," %%a in ('"getmac /v /fo csv /nh | findstr
Local"') do echo The MAC Address is %%a
I added /nh to get rid of the header since it isn't useful in this
context, and had findstr look for Local instead of Ethernet. Hopefully,
if any machines end up with multiple lines in their getmac output there
won't be multiple containing the string Local.
Regards,
Dave
Fair 'nuff.
I'd suggest that it would be logical for the report to follow a specific
structure, but then we are talking Microsoft, so there's no guarantee.
You mentioned you'd used XP for your initial approach. My XP/H system
doesn't include GETMAC - no idea whether it's included or a "resource pack"
or "toolkit" inclusion with other editions or versions.
For a version that should work under XP - assuming that the MAC address is
the first "Physical Address" following "Ethernet adapter Local Area
Connection"
----- batch begins -------
[1]@echo off
[2]set mac=&set ealac=
[3]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
/c:"Ethernet adapter" /c:"Physical Address" ' ) do (
[4]if defined ealac if not defined mac set mac=%%j
[5]if not defined ealac if "%%i"=="Ethernet adapter Local Area Connection"
set ealac=Y
[6])
[7]echo MAC address found is %mac%
[8]set ealac=
------ batch ends --------
>I'd suggest that it would be logical for the report to follow a specific
>structure, but then we are talking Microsoft, so there's no guarantee.
>
>You mentioned you'd used XP for your initial approach. My XP/H system
>doesn't include GETMAC - no idea whether it's included or a "resource pack"
>or "toolkit" inclusion with other editions or versions.
Seems that it's an XP Pro tool. A monumentally stupid thing for MS to do
and create multiple versions of an OS, but which they have perpetuated with
Vista. </rant>
>For a version that should work under XP - assuming that the MAC address is
>the first "Physical Address" following "Ethernet adapter Local Area
>Connection"
>
>----- batch begins -------
>[1]@echo off
>[2]set mac=&set ealac=
>[3]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
>/c:"Ethernet adapter" /c:"Physical Address" ' ) do (
>[4]if defined ealac if not defined mac set mac=%%j
>[5]if not defined ealac if "%%i"=="Ethernet adapter Local Area Connection"
>set ealac=Y
>[6])
>[7]echo MAC address found is %mac%
>[8]set ealac=
>------ batch ends --------
It needs some extra logic - here is what mine says:
Ethernet adapter Local Area Connection 4:
<snip>
> I'd suggest that it would be logical for the report to follow a
> specific structure, but then we are talking Microsoft, so there's no
> guarantee.
As Harlan pointed out, there are at least some circumstances where the
Ethernet Adapter isn't the first listed. I suspect there is a
"convention" of sorts that is followed, but it may not be something that
makes sense to you and I.
>
> You mentioned you'd used XP for your initial approach. My XP/H system
> doesn't include GETMAC - no idea whether it's included or a "resource
> pack" or "toolkit" inclusion with other editions or versions.
It is included in XP Pro, so must be one of the things MS decided that
Home users don't need, like Domains. Sigh.
>
> For a version that should work under XP - assuming that the MAC
> address is the first "Physical Address" following "Ethernet adapter
> Local Area Connection"
>
> ----- batch begins -------
> [1]@echo off
> [2]set mac=&set ealac=
> [3]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
> /c:"Ethernet adapter" /c:"Physical Address" ' ) do (
> [4]if defined ealac if not defined mac set mac=%%j
> [5]if not defined ealac if "%%i"=="Ethernet adapter Local Area
> Connection" set ealac=Y
> [6])
> [7]echo MAC address found is %mac%
> [8]set ealac=
> ------ batch ends --------
Works fine on the two XP Pro machines I have available to test. Thanks
for the code!
Regards,
Dave
Thought they learned that lesson with DOS5 (well, let's forget DOS4 - and
the way it's beginning to look, forget Vista, too)
>
>
> It needs some extra logic - here is what mine says:
>
> Ethernet adapter Local Area Connection 4:
>
>
Curious. Any clue as to why "4"? Are there 0..3 as well, or just "4" for the
sake of "4"?
How about
----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]set mac=&set ealac=
[4]for /f "tokens=1,2 delims=:" %%i in ( ' ipconfig /all^|findstr
/c:"Ethernet adapter" /c:"Physical Address" ' ) do (
[5]if defined ealac if not defined mac set mac=%%j
[6]if not defined ealac echo %%i|find "Ethernet adapter Local Area
Connection" >nul&if not errorlevel 1 set ealac=Y
[7])
[8]echo MAC address found is %mac%
------ batch ends --------
(relying on "E.a.L.A.C" being unique for the block of ;ines in question -
but why "4"???)
>> It needs some extra logic - here is what mine says:
>>
>> Ethernet adapter Local Area Connection 4:
>
>Curious. Any clue as to why "4"? Are there 0..3 as well, or just "4" for the
>sake of "4"?
Every time you change LAN cards it will increment the number - ditto with Wireless connections.
Test your code with this ipconfig /all output - it did find the right MAC address but as you
can see the description field can contain text that could throw the code:
Windows IP Configuration
Host Name . . . . . . . . . . . . : Number5isalive
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : home
Ethernet adapter VMware Network Adapter VMnet8:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
Physical Address. . . . . . . . . : 00-51-55-C0-32-3E
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.135.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter VMware Network Adapter VMnet1:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
Physical Address. . . . . . . . . : 00-51-55-C0-EA-DB
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.159.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter Local Area Connection 4:
Connection-specific DNS Suffix . : home
Description . . . . . . . . . . . : Atheros L1 Gigabit Ethernet 10/100/1000Base-T Controller
Physical Address. . . . . . . . . : 00-1A-FE-8F-EB-AA
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 192.168.1.103
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 192.168.1.1
DHCP Server . . . . . . . . . . . : 192.168.1.1
DNS Servers . . . . . . . . . . . : 202.34.130.33
203.37.230.23
Lease Obtained. . . . . . . . . . : Friday, 29 February 2008 00:17:08
Lease Expires . . . . . . . . . . : Saturday, 1 March 2008 00:17:08
Forget DOS4? FWIW, that one was entirely IBM's fault. I remember the
DOS4 shell which had an option to list all files without their
annoying directory paths just like an MVS disk catalog. What an OS!
> Here's a one-liner using "getmac" instead of "ipconfig":
>
> for /f "tokens=3 delims=," %a in ('"getmac /v /fo csv | findstr
> Ethernet"') do echo %a
To download "getmac" and other Resource Kit tools:
http://support.microsoft.com/kb/927229
> As to alt.msdos.batch.nt, I would normally have posted there but the
> news provider I have available to me at work is down, so for now I'm
> limited to the public Microsoft groups.
I also frequent that group. You can post to it from groups.google.com,
but some choose not to read any posts from that source.
There is that, (although I think most are willing to read, and even
respond if the poster behaves reasonably well), but after a lifetime of
using newsreaders of one variety or another I find the Google Groups web
interface unmanageable. Old dog / new tricks perhaps, but for the odd
post during the (hopefully) short time I'll be without my regular news
provider, if there is an alternative to Google Groups I'll use it.
Regards,
Dave