Home > VMware, Virtualization > VMware guest mac address

VMware guest mac address

March 18th, 2010 jmguilla
It is part of my job to programmatically manage virtual environments. Finding a way to determine if the virtual machines are correctly up on the network is important. To open a RDP and check it by hand is pretty boring… Fortunatly, many APIs give developpers the capability to find NIC’s mac address, guest IP…
One of the development environments one can use to manage VMware virtualization environement is VMware VIX. We can happily see that it exists a mean to get guest IP ( OK, it is not really enlightened anywhere in the API, but after a little of google… it came the getGuestProperty “guestinfo.ip”… ) but nothing was done regarding NIC’s mac address.

The best way I found so far, is to log in guest and run a script that will set guest properties to the found addresses. Vix API makes us able to do such things, the hot point is “what script should I run in guest?”. For every linux based OSes, it is straight forward:

ifconfig | sed -rn ’s/.*(([0-9a-fA-F]{2}[-:]){5}[0-9a-fA-F]{2})/\1/p’ | while read mac ; do index=$(($index+1)); vmware-guestd –cmd “info-set guestinfo.guestmac.$index $mac”; done

Once the script has run successfully, just iterate through guestinfo.guestmac.[1 2 3 ... ] properties until one gets null or empty string value. It was more complicated for Windows OSes, first because I’m ( or I was, this will maybe be the subject of an upcoming post ) a Linux fan boy, but also because the default command interpreter ( cmd.exe, which is only a dos emulator ) has a really “poor” default set of commands. Furthermore, we wanted that to work both on desktop & server Microsoft windows. Here is the script:

VMwareService –cmd “info-set guestinfo.guestmac 0″
ipconfig /all|findstr [0-9a-fA-F][0-9a-fA-F][-:][0-9a-fA-F][0-9a-fA-F][-:][0-9a-fA-F][0-9a-fA-F][-:][0-9a-fA-F][0-9a-fA-F][-:] > %TEMP%\\PA_MAC.txt
SET /p pa_vmm_tmp=<%TEMP%\\PA_MAC.txt
VMwareService –cmd “info-set guestinfo.guestmac %pa_vmm_tmp%

As you may have noticed, once you get the guest property, it remains a little regexp to get the expected mac addresses… oops, sorry, the mac address. Yeah, this is the pitfall, this will make you able to get only one mac address. I hope somebody will give me the right way, Microsoft Windows OSes independent to get every mounted NID’s mac addresses.

I hope this will help!
Comments are closed.