VMware Cloud Community
FreddyFredFred
Hot Shot
Hot Shot
Jump to solution

Get all IPs of a VM

In powershell I can use the VMGuest object and the IP address property to return an array of all IPs on the machine. I can't seem to find anything in vCO which does the same. I see there's a VcVirtualMachineGuestSummary but the ipAddress property only returns 1 IP.

Is there anything in vCO  that will return all IPs?

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, there is.

Script code below will print all IP address on all NICs on a given virtual machine ('vm' is the virtual machine object)

var nics = vm.guest.net; // enumerate all NICs

for (var i in nics) {

  var nic = nics[i];

  var ips = nic.ipConfig.ipAddress; // enumerate all IP addresses on this NIC

  for (var j in ips) {

    var ip = ips[j];

    System.log("IP -> " + ip.ipAddress);

  }

}

View solution in original post

3 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, there is.

Script code below will print all IP address on all NICs on a given virtual machine ('vm' is the virtual machine object)

var nics = vm.guest.net; // enumerate all NICs

for (var i in nics) {

  var nic = nics[i];

  var ips = nic.ipConfig.ipAddress; // enumerate all IP addresses on this NIC

  for (var j in ips) {

    var ip = ips[j];

    System.log("IP -> " + ip.ipAddress);

  }

}

FreddyFredFred
Hot Shot
Hot Shot
Jump to solution

Thanks! That did the trick.

0 Kudos
rdytoautomate
Contributor
Contributor
Jump to solution

Is there a way to get a breakdown by NIC #?  Essentially, I need the following type of output:

Nic1: network_name (port group), IPv4 address, ipv6 address, mac address

Nic2: network_name (port group), IPv4 address, ipv6 address, mac address

...

Nic10: network_name (port group), IPv4 address, ipv6 address, mac address

I have the NIC to port group mapping completed using the following code, but not seeing how to get the same type of results for mac address, ipv4 address, and ipv6 address:

var devices = vm.config.hardware.device;

for (var i in devices) {

  if (System.getModule("com.vmware.library.vc.vm.network").isSupportedNic(devices[i])) {

  if (--nicNumber == 0) {

  if (devices[i].backing instanceof VcVirtualEthernetCardNetworkBackingInfo) {

  return VcPlugin.convertToVimManagedObject(vm ,devices[i].backing.network);

  } else if (devices[i].backing instanceof VcVirtualEthernetCardDistributedVirtualPortBackingInfo) {

  var portgroupId = null;

  var portgroupKey = devices[i].backing.port.portgroupKey;

  var networks = vm.network;

  for (var j in networks) {

  var network = networks[j];

  if (network instanceof VcDistributedVirtualPortgroup) {

  if (network.key == portgroupKey) {

  portgroupId = network.id;

  break;

  }

  }

  }

  if (portgroupId == null) {

  return null;

  }

  var moRef = new VcManagedObjectReference();

  moRef.type = "DistributedVirtualPortgroup";

  moRef.value = portgroupId;

  System.log("The Port Group for Nic 1: " + moRef.value);

  return VcPlugin.convertToVimManagedObject(vm ,moRef);

  } else {

  return null;

  }

  }

  }

}

return null;

0 Kudos