Gain MAC address by batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chenry
Posts: 3
Joined: 30 Dec 2013 02:24

Gain MAC address by batch file

#1 Post by Chenry » 30 Dec 2013 19:37

Hi all,
I have a query and need your help, geniuses.
I'm a junior helpdesk in our company, and I have to record all the machines's name & MAC address when I prepare them. To be relaxable, I want to get the information by a script. Now the problem is: there are more than one network adapters, including the virtual adapter. I use below script:
@echo off
echo %date% %time% >>\\fileserver\MACList.txt
echo ============================ >>\\fileserver\MACList.txt
ipconfig /all | find /i "host name" >>\\fileserver\MACList.txt
ipconfig /all | find /n /i "physical address" >>\\fileserver\MACList.txt
echo ============================ >>\\fileserver\MACList.txt
echo. >>\\fileserver\MACList.txt


Owing to the multiple adapter, I only can show the line number, and filter the correct one manually. Now my requirement is, how can I only record the appropriate physical address? It's usually the first one.

Thank you in advance and wish a good New Year.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Gain MAC address by batch file

#2 Post by berserker » 30 Dec 2013 19:50

if you have getmac something like this

Code: Select all

@echo off
For /f "tokens=1" %%a in ('getmac /NH') do  (
 echo %%a
)

or this

Code: Select all

For /f "tokens=2 delims=:" %%a in ('ipconfig /all ^| findstr /i "physical"') do  (
 echo %%a
)


On a side note:
you will probably need to do string and file parsing/manipulation in your tasks, so IF YOU CAN USE non native tools, try to get a a couple of good tools to use for string manipulation. In the unix world, awk, sed are famous for string manipulation and they have been ported to windows. I have a collection here

https://www.dropbox.com/sh/cja5mvymvinmfco/mHw6J4I1Kd

download awk.exe and then you can do this

Code: Select all

For /f %%a in ('ipconfig /all ^| awk -F":" "/Physical/{print $2;exit}"') do  (
 echo mac %%a
)
Last edited by berserker on 31 Dec 2013 09:11, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Gain MAC address by batch file

#3 Post by penpen » 31 Dec 2013 06:10

Chenry wrote:Owing to the multiple adapter, I only can show the line number, and filter the correct one manually. Now my requirement is, how can I only record the appropriate physical address? It's usually the first one.
So it is not always the first one?
How do you differ between the virtual adapter and the physical one manually?
Has the virtual adapter in different systems always the same name?

penpen

zpimp
Posts: 12
Joined: 14 Jun 2013 12:58

Re: Gain MAC address by batch file

#4 Post by zpimp » 31 Dec 2013 12:32

there was a wmic command wich would give you the real mac address, and only one, but i dont have it now
similar commands are available for system serialnumber and hdd serial number

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Gain MAC address by batch file

#5 Post by siberia-man » 03 Jan 2014 20:24

Code: Select all

ipconfig /all | filter_command "physical"

Filtering (by find, findstr, awk or something else) of the ipconfig output is not reliable way because of dependency on user-defined and locale settings. But you can use it if you sure that all machines have identical locale settings.

The following command can give you more details for analysis and it is locale independent (in common casesthe )

Code: Select all

wmic nicconfig where "ipenabled='true'"


The following command gives you the MAC address only

Code: Select all

wmic nicconfig where "ipenabled='true'" get macaddress

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Gain MAC address by batch file

#6 Post by Ed Dyreen » 04 Jan 2014 15:06

siberia-man wrote:

Code: Select all

wmic nicconfig where "ipenabled='true'"

Interesting, filtering ( defaultIPGateway IS NOT NULL ) then identifies the MAC used to connect the network. This filters my virtual networks and works in any language thanks :)

Chenry
Posts: 3
Joined: 30 Dec 2013 02:24

Re: Gain MAC address by batch file

#7 Post by Chenry » 14 Jan 2014 22:47

So many experts :D :D Thank you all friends.

Post Reply