3

I need to write a script that would get hostname and all physical addresses on my local machine. It will be run on the several machines. After some research I've found out that I can get hostname from the $HOSTNAME variable and I also found getmac command. The problem with getmac is that it returns all mac addresses (loopback, logical, etc.) without any clue about interface it uses. Here is my example output:

2C-27-D7-40-62-7A   \Device\Tcpip_{A04C5C9D-CD41-48D1-8EC8-54128C70835C}
32-9D-DE-C7-3F-6D   \Device\Tcpip_{EB38E492-9BEF-4F8F-98E6-C08716920461}
02-90-4C-4F-4F-50   \Device\Tcpip_{42DB7C91-BB9E-4BF4-B374-289F1BBA7AC6}

It gives me 3 mac addresses, from which only one is physical, but I don't see the way to decide which one. Could you please help me?

Jandrejc
  • 499
  • 3
  • 9
  • 18

3 Answers3

3

since you mentioned cygwin, 'awk' would work over it... for general windows execution even a separate download of GNU-Win32 utils is available with AWK.

when you do "$ getmac -V -FO LIST" it would display the Network Devices with their name and then you could fetch the required MAC Address as following:

[Ethernet Card]

for "Local Area Connection" text-marked card ~

$ getmac -V -FO CSV | awk -F ',' '{if(match($1,"Local Area Connection"))print $3;}'

[Wireless Card]

for "Wireless Network Connection" text-marked card ~

$ getmac -V -FO CSV | awk -F ',' '{if(match($1,"Wireless Network Connection"))print $3;}'

if you have more than one Ethernet/Wireless cards, name might change a bit... you can check for actual name using "$getmac -V" and match it as required.

AbhishekKr
  • 300
  • 2
  • 12
2

Just parse the output from ipconfig /all

I presume you are only interested in the "real" adapter connected to the outside.. Based on the IP-address/default gateway info that is also in there it should be relatively straightforward to determine which is which.

Be warned that the ipconfig output varies slightly from Windows to Windows version. You are fine if you split the lines on the ":". Don't assume that the : always appears in the same column.

Tonny
  • 671
  • 5
  • 17
0

You can also parse the output of ipconfig /all which is a windows utility that will give you interface names as well as their MAC addresses. I'm not 100% sure which Windows platforms support this utility, but it does exist on systems from NT through XP.

It does execute from a Cygwin bash shell.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • Is it the only option? I'm trying to avoid parsing – Jandrejc Sep 01 '11 at 10:32
  • 2
    I don't know of any method to avoid parsing. Windows just doesn't have decent commandline tools for this sort of thing. Ipconfig is relatively easy to do (even in plain Windows CMD script). Alternative would be quering the WMI data from Powershell which is much more complicated. – Tonny Sep 01 '11 at 10:33