Search code examples
cygwinhostnamemac-address

Getting Mac Address and Hostname with Cygwin


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?


Solution

  • 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.