2 Replies Latest reply on Sep 29, 2018 12:15 PM by aenagy

    How to find management IP(s) of ESXi host from VcHostSystem (vRO 7.3 and vSphere 6.0)

    aenagy Hot Shot

      This (How to find management IP(s) of ESXi host from VcHostSystem (vRO 7.3 and vSphere 6.0) ) is being re-posted here.

       

      I am trying to find the ESXi host to which a virtual machine is registered. From the vm I am able to find the host (VcHostSystem) in vRO. Searching the API it would appear that I need HostVirtualNicManagerNicType (http://pubs.vmware.com/vsphere-6-0/topic/com.vmware.wssdk.apiref.doc/vim.host.VirtualNicManager.NicType.html). The code below allows me to iterate over the virtual NICs and includes IP address information but I haven't figured out how to definitively determine which interface(s), and therefore which IP(s), are designated for ESXi management.

       

      var          EsxiHostConfigNetworkdVnic = vm.runtime.host.config.network.vnic  
      System.log( "EsxiHostConfigNetworkdVnic.length = " + EsxiHostConfigNetworkdVnic.length )  
        
      for( var VnicIndex = 0 , VnicLength = EsxiHostConfigNetworkdVnic.length ; VnicIndex < VnicLength ; VnicIndex++ ){  
      System.log( "EsxiHostConfigNetworkdVnic[" + VnicIndex + "] = " + EsxiHostConfigNetworkdVnic[ VnicIndex ] )  
      }  
      

       

      Thanks in advance.

        • 1. Re: How to find management IP(s) of ESXi host from VcHostSystem (vRO 7.3 and vSphere 6.0)
          iiliev Champion
          Community WarriorsVMware Employees

          Something like the following should work:

           

          var vnicManager = vm.runtime.host.configManager.virtualNicManager;
          var netConfig = vnicManager.queryNetConfig(VcHostVirtualNicManagerNicType.management);
          System.log("Selected vNics: " + netConfig.selectedVnic);
          System.log("Candidate vNics: ");
          for each (var vNic in netConfig.candidateVnic) {
            if (netConfig.selectedVnic.indexOf(vNic.key) == -1) {
              System.log("vNic[ " + vNic.key + " ] is not selected; skipping it");
              continue;
            }
            System.log("vNic[ " + vNic.key + " ] is selected");
            System.log(vNic.spec.ip);
          }
          
          • 2. Re: How to find management IP(s) of ESXi host from VcHostSystem (vRO 7.3 and vSphere 6.0)
            aenagy Hot Shot

            Ilian Iliev:

             

            That worked perfectly. My final action code looks like this:

             

            // INPUT:
            //   vcHostSystem (VcHostSystem)
            
            
            var actionResult = ""
            var vnicManager = vcHostSystem.configManager.virtualNicManager
            var netConfig = vnicManager.queryNetConfig(VcHostVirtualNicManagerNicType.management);  
            
            
            //System.log("Selected vNics: " + netConfig.selectedVnic);  
            //System.log("Candidate vNics: ");  
            for each ( var vNic in netConfig.candidateVnic ) {
                if (netConfig.selectedVnic.indexOf( vNic.key ) == -1) {  
                    //System.log("vNic[ " + vNic.key + " ] is not selected; skipping it");  
                } else {
                    //System.log("vNic[ " + vNic.key + " ] is selected");  
                    //System.log(vNic.spec.ip);  
                    actionResult += vNic.key.split( "." )[3].split( "-" )[1] + " IP: " + vNic.spec.ip.ipAddress + "  "
                }
            }  
            return actionResult
            

             

            Thanks.