scheidecker
Contributor
Contributor

How to get the IP address from a host in C#

Jump to solution

Hello,

If I iterate through my datacenters and fetch all theirs hosts into ManagedObjectReference (MORs), how can I obtain each host ip address from their MORs in C#?

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
jrackliffe
Hot Shot
Hot Shot

You are very close though. If you can pull the summary and vm props there should be no reason you can't pull config. And once you have config everything else is just steps away.

View solution in original post

0 Kudos
26 Replies
jeveenj
Enthusiast
Enthusiast

Hi

You can drill down from Host System MOR to its IP property by fetching the following properties:

Host -> configManager -> networkSystem -> networkInfo -> consoleVnic -> spec -> ip -> ipAddress

Hope this helps.

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
scheidecker
Contributor
Contributor

Jeveenj,

In C# I only have a ManagedObjectReference and I cannot access the fields like that. Hence my question but thanks for the atempt.

0 Kudos
TARGON
Contributor
Contributor

scheidecker,

are you by any chance trying to get the host ip address using C#? I am trying a similar thing from a guest VM. The caveat is that my program is not supposed to know anything about the ESX host and somehow needs to find its name or ipaddress. My guest VMs have Windows XP and Windows Vista installed on them. Any insight would be appreciated.

Thanks

0 Kudos
jrackliffe
Hot Shot
Hot Shot

I guess I am confused at that response.

What the parent was giving you was the map to follow from the MOR? Just to be sure you do know how to use a PropertyCollector etc. to get the properties of a Mor correct? That C# code is demonstrated in the samples included wthe SDK distro.

You can also use the Vim.dll included w the PowerShell kit as it has most if not all objects defined in the API.

For the other responder if you are starting w the VM's mor you will need to traverse from the VM's mor -> runtime -> host and then follow the rest of the tree. If you have only one vCenter you can search for your VM and then work your way out. If you have multiple that isfar more difficult. There isn't natively even overt he hidden rpc channel (to my knowledge) in the guest to determine the host.

0 Kudos
scheidecker
Contributor
Contributor

Guys,

Thanks for the help but, like others, I still could not figure out how to do it with C#.

Thanks.

0 Kudos
jrackliffe
Hot Shot
Hot Shot

I have a couple applications doing this everyday in C# so I have no problem giving you a hand on doing it if you still need help. Just need a bit more scope on what you are struggling with.

It is actually relatively trivial once you get past the PropertyCollector stuff or move to the Vim.dll toolkit (which is actually your easiest path)

J

0 Kudos
scheidecker
Contributor
Contributor

I would like o get that code much. What I need is very simple. I have 2 hosts on 2 different datacenters. Once I have the MOR for one host, how can I extract the IP address it is running with. That is all. Basically, a simple function to get its IP address. It seems easier to get the IP address from a VM but not from a host and that is what I am strugling with. Thanks.

0 Kudos
dmitrif
Enthusiast
Enthusiast

Do you know how to retrieve properties with the PropertyCollector?

If you do, a host system IP address can be retrieves from two places.

1. config.network.consoleVnic.spec.ip.ipAddress

works for ESX, the "key" is usually the only key in that collection

2. config.network.vnic.spec.ip.ipAddress

works for ESXi, the "key" is the key of vnic that belongs to the management network portgroup

If you don't know how to retrieve properties with the PropertyCollector then read documentation how to do that.

D.
0 Kudos
scheidecker
Contributor
Contributor

Hi, No I do not know how to do that yet. (PropertyCollector)

0 Kudos
dmitrif
Enthusiast
Enthusiast

You've got to learn that, man, this is the very foundation.

Check out sample that came with SDK and read the Programmers Guide

D.
0 Kudos
jrackliffe
Hot Shot
Hot Shot

While I agree w parent that without an understanding of the compexity in the PropertyCollector model you will not be able to use the SOAP interface natively, there is another option... it is commonly overlooked, though carries a bit of baggage and limits though is a great starter for .NET programmers.

Basically in the PowerShell distro there is minimal doco and a class lib called Vim.dll. This is the relatively unloved ".NET Toolkit" that is used as a foundation for the Powershell distro by Carter et al. It wraps the SOAP interface with many first class objects and makes working a bit easier.

I have tossed in a VERY trivial example doing what you need without a bunch of exception handling etc. Just a demo of using VimClient instead of using the SOPA proxies generated natively. Now the major baggae w getting the ease of use is that Vim.dll dynamically loads ALL of the existing Vim stubs and requires you to reference or GAC all the Vims (20,25,40) and Serializers. If you can install the Powershell kit on your application host it will GAC the Vim libs otherwise you will need to copy them from the GAC (C:\windows\assembly\GAC_MSIL\Vim*). There should be 6 DLLs buried in the GAC with a VimService##.dll and a VimService##.XmlSerializers.dll.

static VimClient vc = new VimClient(); 
vc.Connect("https://virtualcenter.domain.com/sdk");
vc.Login(username, password);

NameValueCollection filter = new NameValueCollection();
filter.Add("name", "^" + "esxhostname" + "$");

HostSystem h = (HostSystem)vc.FindEntityView(typeof(HostSystem), null, filter, null);
Console.WriteLine(h.Config.Network.ConsoleVnic[0].Spec.Ip.IpAddress);

0 Kudos
dmitrif
Enthusiast
Enthusiast

Good point, Justin, using the VMware PowerShell Toolkit .NET library is definitely a good alternative for somebody who does not want to get into the "raw" SDK programming.

But we like it raw and wiggling Smiley Happy

D.
0 Kudos
jrackliffe
Hot Shot
Hot Shot

Well if you want to roll it raw be prepared for a slow slog into the loosely coupled world of VMware. Smiley Wink Although using the SOAP may make you feel like a 31337 h@X0r VMware does publish the toolkits for a reason and they are to make our lives easier. You don't see many of the Perl or Java guys using the SOAP API natively anymore as the toolkits wrap up much of the hard work. I agree the .NET toolkits is not quite up to the level of the Perl or Java, but it is quite useful.

To take on the SOAP I agree that you should take a look at the C# samples that are delivered w the SDK kit as the volume of code needed to use those natively is not quite suited to the forum. Luckily you should be able to use the searchIndex to get your hostmor. Propertyspec is a bit more straight forward than discovering mors in the tree, but it still requires you to write a good amount of code.

The PropertyCollector project in samples/cs should get you 90% of the way there so enjoy!

J

sochry
Enthusiast
Enthusiast

I am using something like the following:

HostSystem host = (HostSystem) vimClient.FindEntityView(typeof (HostSystem), null, null, null);

HostNetworkSystem networkSystem = (HostNetworkSystem) vimClient.GetView(host.ConfigManager.NetworkSystem, null);

HostVirtualNicSpec virtNicSpec = networkSystem.NetworkConfig.Vnic[0].Spec;

vimClient is initialized as a new VimClient. I am using the PowerCLI library.

0 Kudos
dmitrif
Enthusiast
Enthusiast

How do you know that Vnic[0] is the Management Network?



Dmitri Fedorov

BridgeWays, a division of Xandros

www.bridgeways.ca

D.
0 Kudos
scheidecker
Contributor
Contributor

My problem is that I need to fix that on a code that uses Vim25Api. Hence, that suggestion might not work.

Thanks again.

0 Kudos
jrackliffe
Hot Shot
Hot Shot

Very good point... any one of those Vnics are technically Host physical IPs, but may be used in a number of configurations. Do you need the management IP or would any IP do?

0 Kudos
jrackliffe
Hot Shot
Hot Shot

So... you have existing code that you are trying to patch to fix a piece of functionality?

Really not sure I am following specifically what you need and why.

e.g.

Do you have existing WSDL based code you are refactoring or patching?

Vim25Api is the default namespaced used by the VMware Build2005 so have you reviewed the samples around propertycollector? Have you tried to adapt the sample code to at minimul find the hostsystem?

In ref to multple phy adapter question above... what IP do you need and why? Does it matter which IP you get or do you need something specific?

J

0 Kudos
scheidecker
Contributor
Contributor

Hi,

What I need is the IP of the Host. The reason being is that it is one piece of information that I need to save on an asset database table. Those IPs are not supposed to change. I am able to retrieve most of the information doing this:

VmHostInfo hostInfo = new VmHostInfo();

object[] info = GetProperties((ManagedObjectReference)host[0], new string[] { "summary", "vm" });

HostListSummary hostSummary = (HostListSummary)info[0];

hostInfo.Name = hostSummary.config.name;

hostInfo.ProcessorType = hostSummary.hardware.cpuModel;

hostInfo.UUID = hostSummary.hardware.uuid;

hostInfo.VirtualMachines = new List<string>();

hostInfo.IPAddress = hostSummary.managementServerIp;

The problem is that the hostSummary.managementServerIp does not return the correct Host IP but the IP od the ESX server. Sometimes I even get that blank for different hosts.

0 Kudos