You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取VM网卡IP?vSphere JDK Java 6.5技术咨询

获取vSphere VM的IP地址(vSphere JDK 6.5)

Hey there! The VM's IP addresses aren't stored in the VMTypes.Info or EthernetTypes.Info classes you're currently using—those hold static configuration details for the VM and its NICs. IPs are runtime guest OS information, so you need to pull them from the guest info APIs.

所需类和获取方式

In vSphere JDK 6.5, you'll want to use the GuestTypes.Info class, which you can retrieve via the VmGuest service. This class contains all the runtime data reported by VMware Tools, including the VM's IPv4/IPv6 addresses.

代码示例(结合你的现有代码)

Here's how to integrate this into your existing code to fetch IP addresses:

VMTypes.Info vmInfo = vcVAPIConnect.getVM(vapiStub, vmID);
if (vmInfo != null) {
    System.out.println("VM Detail Id = " + vmID);
    System.out.println("VM Detail Name = " + vmInfo.getName());
    Set<String> fldList = vmInfo._getDynamicFieldNames();
    System.out.println("VM Dyn. Fieldlist (Size): " + fldList.size());
    for (String fldValue: fldList) {
        System.out.println("VM Detail Name = " + fldValue + " / " + vmInfo._getDynamicField(fldValue));
    }
    System.out.println("VM Detail Hardware Version = " + vmInfo.getHardware().getVersion());
    System.out.println("VM Detail CPU CoresPerSocket = " + vmInfo.getCpu().getCoresPerSocket());
    System.out.println("VM Detail CPU Count = " + vmInfo.getCpu().getCount());
    System.out.println("VM Detail CPU CannonialName = " + vmInfo.getCpu()._getCanonicalName());
    System.out.println("VM Detail RAM Size = " + vmInfo.getMemory().getSizeMiB());
    System.out.println("VM Detail RAM CannonialName = " + vmInfo.getMemory()._getCanonicalName());
    Map<String, EthernetTypes.Info> vmNics = vmInfo.getNics();
    System.out.println("VM Detail NIC Size = " + vmNics.size());
    for (String nicKey: vmNics.keySet()) {
        System.out.println("VM Detail NIC Key = " + nicKey);
        System.out.println("VM Detail NIC CannonialName = " + vmNics.get(nicKey)._getCanonicalName());
        System.out.println("VM Detail NIC MacAddr = " + vmNics.get(nicKey).getMacAddress());
        System.out.println("VM Detail NIC MacType = " + vmNics.get(nicKey).getMacType().toString());
        System.out.println("VM Detail NIC Label = " + vmNics.get(nicKey).getLabel());
        System.out.println("VM Detail NIC PCI Slot = " + vmNics.get(nicKey).getPciSlotNumber());
        System.out.println("VM Detail NIC Backing Name = " + vmNics.get(nicKey).getBacking().getNetworkName());
        System.out.println("VM Detail NIC Backing Net = " + vmNics.get(nicKey).getBacking().getNetwork());
        System.out.println("VM Detail NIC Backing Host = " + vmNics.get(nicKey).getBacking().getHostDevice());
        fldList = vmNics.get(nicKey)._getDynamicFieldNames();
        System.out.println("VM Detail NIC Dyn. Fieldlist (Size): " + fldList.size());
        for (String fldValue: fldList) {
            System.out.println("VM Detail NIC = " + fldValue + " / " + vmInfo._getDynamicField(fldValue));
        }
    }

    // --------------------------
    // 新增:获取Guest信息及IP地址
    // --------------------------
    VmGuest vmGuestService = VmGuestFactory.getService(vapiStub);
    GuestTypes.Info guestInfo = vmGuestService.get(vmID);

    if (guestInfo != null) {
        System.out.println("\n--- Guest OS & IP Address Details ---");
        System.out.println("Guest OS Family: " + guestInfo.getGuestFamily());
        System.out.println("Guest OS ID: " + guestInfo.getGuestId());

        List<GuestTypes.IpAddressInfo> ipAddresses = guestInfo.getIpAddresses();
        if (!ipAddresses.isEmpty()) {
            System.out.println("Assigned IP Addresses:");
            for (GuestTypes.IpAddressInfo ipInfo : ipAddresses) {
                System.out.printf("- IP: %s | Type: %s | DHCP Assigned: %b%n",
                    ipInfo.getIpAddress(),
                    ipInfo.getType().name(), // 显示IPv4/IPv6
                    ipInfo.getIsDhcp()
                );
            }
        } else {
            System.out.println("No IP addresses detected. Ensure VMware Tools is running on the VM!");
        }
    } else {
        System.out.println("Failed to retrieve guest information. Check if VMware Tools is installed and active on the VM.");
    }
}

关键注意事项

  • VMware Tools依赖: The guest OS data (including IPs) is only available if VMware Tools is installed and running on the VM. Without it, the API won't return any runtime guest info.
  • IP类型: The GuestTypes.IpAddressInfo class gives you both IPv4 and IPv6 addresses, plus details like whether the IP was assigned via DHCP.

内容的提问来源于stack exchange,提问作者Volker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:38:45