Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 723
Jandrejc Avatar asked Nov 27 '25 09:11

Jandrejc


2 Answers

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.

like image 83
AbhishekKr Avatar answered Dec 02 '25 06:12

AbhishekKr


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.

like image 32
Tonny Avatar answered Dec 02 '25 07:12

Tonny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!