Page 1 of 1

Gain MAC address by batch file

Posted: 30 Dec 2013 19:37
by Chenry
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.

Re: Gain MAC address by batch file

Posted: 30 Dec 2013 19:50
by berserker
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
)

Re: Gain MAC address by batch file

Posted: 31 Dec 2013 06:10
by penpen
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

Re: Gain MAC address by batch file

Posted: 31 Dec 2013 12:32
by zpimp
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

Re: Gain MAC address by batch file

Posted: 03 Jan 2014 20:24
by siberia-man

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

Re: Gain MAC address by batch file

Posted: 04 Jan 2014 15:06
by Ed Dyreen
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 :)

Re: Gain MAC address by batch file

Posted: 14 Jan 2014 22:47
by Chenry
So many experts :D :D Thank you all friends.