VMware {code} Community
werw
Contributor
Contributor
Jump to solution

Is there any way to programmatically retrieve the MAC address of a VMware virtual machine

Can someone suggest to programmatically retrieve the MAC address of a VMware virtual machine.

I need to put in a code for the above functionality.

Message was edited by: AntonVZhbankov

Corrected Hyper-V to VMware

Reply
0 Kudos
1 Solution

Accepted Solutions
daniellynn
Enthusiast
Enthusiast
Jump to solution

Now that I have some code in front of me, you can basically try this:


// MAC Variable
string MACAddress;

// Get Config Info (hardware)
vmConfigInfo = (VirtualMachineConfigInfo)myAPI.getProperties(vmRef, new String[1] {"config"});

//Iterate through devices
foreach (VirtualDevice vDv in vmConfigInfo.hardware.device)
{
    try
    {
        VirtualEthernetCard mycard = (VirtualEthernetCard)vDv;
        MACAddress = mycard.macAddress;
    }
    catch
    {
    }
}

Basically what is happening hear is that you're pulling all of the virtual hardware pieces and iterating through them. If the piece is a network card, this code will be able to cast it as one and pull the MAC. Otherwise, the cast will fail and it'll move on. If you have more than one network card, you'd want to add the mac to a string array or something to account for more than one.

This code assumes you've connected and you have a means to pull the config property from the VM. The "getProperty" function in this code refers to my own function, not one in VMWare. If you're not sure on how to do that, just post back to here and I know there are sample pages out there someone can post to explain that part.

View solution in original post

Reply
0 Kudos
14 Replies
athlon_crazy
Virtuoso
Virtuoso
Jump to solution

Are you sure you are in the right forum?

vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org
Reply
0 Kudos
werw
Contributor
Contributor
Jump to solution

Thanks athlon_craz for bringing it to my notice.

Rephrasing the question.

Is there any way to programmatically retrieve the MAC address of a VMware virtual machine?

Reply
0 Kudos
continuum
Immortal
Immortal
Jump to solution

what about looking it up in the vmx-file ?




___________________________________

VMX-parameters- WS FAQ -[ MOAcd|http://sanbarrow.com/moa241.html] - VMDK-Handbook


________________________________________________
Do you need support with a VMFS recovery problem ? - send a message via skype "sanbarrow"
I do not support Workstation 16 at this time ...

Reply
0 Kudos
werw
Contributor
Contributor
Jump to solution

But is there a way to retrieve the MAC address with the help of a piece of code?

Reply
0 Kudos
athlon_crazy
Virtuoso
Virtuoso
Jump to solution

you can start with this :

$cat /vmfs/volumes/../VMname.vmx | grep 00: | awk '{ print $3 }'

vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org

http://www.no-x.org
Reply
0 Kudos
werw
Contributor
Contributor
Jump to solution

@

Can please elaborate a little?

Actually I have started a little back and kind of new to this

technology.

My requirement is to put a code in my prograam to retrive the MAC addres of any of the stated Vmware machine.

Kind of a function where I can feed some detail about the Vmware virtual machine and get back the MAC address.

Reply
0 Kudos
athlon_crazy
Virtuoso
Virtuoso
Jump to solution

From previous command that I gave, you actually can make at least a script as below :

# sh vmmac.sh

Please enter your Virtual Machine .VMX

/vmfs/volumes/6ca25c14-0b2d8c50/dns1/ftp.vmx

Your VM mac address is:

"00:0c:29:6b:41:dc"






vcbMC-1.0.6 Beta

vcbMC-1.0.7 Lite

http://www.no-x.org

http://www.no-x.org
Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

When you say "programmatically" ... can you further elaborate on this? What scripting or programming language are you looking to implement this solution in? There are various ways of accomplishing this from simple shell script running within an ESX host to using vSphere's various SDK bindings such as: Perl, Powershell, C#/.NET, Java, etc.

For example, here is a vSphere SDK for Perl solution that retrieves the MAC Address of all VMs whether you're connecting to a vCenter server or ESX/ESXi host.

=========================================================================

William Lam

VMware vExpert 2009

VMware scripts and resources at:

Twitter: @lamw

Getting Started with the vMA (tips/tricks)

Getting Started with the vSphere SDK for Perl

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
werw
Contributor
Contributor
Jump to solution

I am looking forward to implement it in C# language.

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Yep, there is a C# binding: vSphere WebServices SDK for C#

If you're new to the VMware API/SDKs, you'll want to take a look at this getting started document:

Secondly, you'll want to check out the developer forum for resources - http://communities.vmware.com/community/developer/learn and also check out Code Central which will have snippets of code or full on scripts/programs using the various language bindings, including C# - http://communities.vmware.com/community/developer/codecentral

=========================================================================

William Lam

VMware vExpert 2009

VMware scripts and resources at:

Twitter: @lamw

Getting Started with the vMA (tips/tricks)

Getting Started with the vSphere SDK for Perl

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
wila
Immortal
Immortal
Jump to solution

Moved thread to vSphere Web Services SDK forum.



--
Wil
_____________________________________________________
VI-Toolkit & scripts wiki at http://www.vi-toolkit.com

Contributing author at blog www.planetvm.net

Twitter: @wilva

| Author of Vimalin. The virtual machine Backup app for VMware Fusion, VMware Workstation and Player |
| More info at vimalin.com | Twitter @wilva
Reply
0 Kudos
daniellynn
Enthusiast
Enthusiast
Jump to solution

First, the MAC is on the virtual NIC, not the VM. This distinction only matters if you have more than one NIC, but I thought I'd note it.

Also, have you used the C# web service API at all yet? Specifically, have you pulled properties from managed objects? If so, I can just point you in the right direction. Otherwise, I'll try to give a more comprehensive answer.

Reply
0 Kudos
daniellynn
Enthusiast
Enthusiast
Jump to solution

Now that I have some code in front of me, you can basically try this:


// MAC Variable
string MACAddress;

// Get Config Info (hardware)
vmConfigInfo = (VirtualMachineConfigInfo)myAPI.getProperties(vmRef, new String[1] {"config"});

//Iterate through devices
foreach (VirtualDevice vDv in vmConfigInfo.hardware.device)
{
    try
    {
        VirtualEthernetCard mycard = (VirtualEthernetCard)vDv;
        MACAddress = mycard.macAddress;
    }
    catch
    {
    }
}

Basically what is happening hear is that you're pulling all of the virtual hardware pieces and iterating through them. If the piece is a network card, this code will be able to cast it as one and pull the MAC. Otherwise, the cast will fail and it'll move on. If you have more than one network card, you'd want to add the mac to a string array or something to account for more than one.

This code assumes you've connected and you have a means to pull the config property from the VM. The "getProperty" function in this code refers to my own function, not one in VMWare. If you're not sure on how to do that, just post back to here and I know there are sample pages out there someone can post to explain that part.

Reply
0 Kudos
werw
Contributor
Contributor
Jump to solution

Thanks .

This seems to what I was looking for.

Will post further in case of any queries.

Thanks Guys.

Reply
0 Kudos