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
26 Replies
jrackliffe
Hot Shot
Hot Shot

Well "IP of the Host" could be an issue as an ESX could have many IPs based upon how you configured it. Lets just say any of the Hosts potential IPs wouldbe fine for arguement's sake though.

Now managementServerIp is the VirtualCenter's IP and not the ESX hosts IP (always check the docks).

So instead of the HostListSummary you will need todo another GetProperties call with "config" as the propertyName which will return a HostConfigInfo. From there you can follow the Network, consoleVnic, etc. That array of devices corresponds to the "actual" physical adapters associated w the Host.

Make sense?

scheidecker
Contributor
Contributor

It does. So I call "config" on the properties and cast it as HostConfigInfo, then get the values from there. That is what I wanted. I've tried to do that sometime ago but had casting exceptions, probablyused the wrong object. Our server is down so I cannot test now but I will do that as soon as I can. Thanks.

0 Kudos
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
scheidecker
Contributor
Contributor

Well, unfortunately I do not have the server here with me to test, but that would be something like, correct?

object[] configInfo = GetProperties((ManagedObjectReference)host[0], new string[] { "config" });

HostConfigInfo hostConfigInfo = (HostConfigInfo) configInfo[0];

//hostConfigInfo.network.

Now, on the hostConfigInfo.network I have the vnic array and the pnic array. One is the virtual and the other is the physical. So, to get the IP which one should be used? Of course, I will debug that once I can but just wondering where the value would be.

0 Kudos
jrackliffe
Hot Shot
Hot Shot

Ok, first off always try and wrap your code in the element so that Jive doesn't mess it up. Just put one of those tags on the beginning and end and it should make it a special block.

Now guessing at what wasn't Jived it looks like you are good, but you need a host or vCenter to confirm. Also I think the consoleVnic is the one you want.

J

0 Kudos
scheidecker
Contributor
Contributor

OK guys,

I was able to figure that out, thanks.

Basically you need to extract the config property and cast it into a HostConfigInfo.

Form the HostConfigInfo object you need to get the array of virtual NICs hostConfigInfo.Network.vnic.

The first NIC on the VNIC array has the host ip address and to extract that you do nic.spec.ipAddress.

The pnics, Physical Nics do not retinr the information needed: ie ip address. Also the consoleNic does not have that either.

Problem solved.

0 Kudos
jrackliffe
Hot Shot
Hot Shot

In my best Futurama voice... "Good News Everyone!"

Yeah from a process improvment, especially with properities, I highly recommend using the mob browser to traverse the object hierarchy looking for that piece of data. Then it is easy to eliminate dead ends as there are plenty of properties that from their names or descriptions n the doco should have the data you want, but don't. Then you can take the web traversal and just port that to your C# code.

J

0 Kudos