使用VMware VSphere WebService SDK进行开发 (五)——根据虚拟机的名称获取对应主机的IP地址

简介: 在整个获取监视信息的过程中,最难获取的就是根据虚拟机的名称获得对应主机的IP地址的功能。(个人觉得比较绕,绕了好久我才找到) 首先根据虚拟机的名称获得对应主机(HostSystem)的ManagedObjectReference对象。

在整个获取监视信息的过程中,最难获取的就是根据虚拟机的名称获得对应主机的IP地址的功能。(个人觉得比较绕,绕了好久我才找到)

首先根据虚拟机的名称获得对应主机(HostSystem)的ManagedObjectReference对象。

RetrieveResult props = getRetrieveResultObjectWithProperty("VirtualMachine", "summary.runtime.host");
		ManagedObjectReference mor = null;
		if (props != null)
		{
			Boolean flag = false;
			for (ObjectContent oc : props.getObjects())
			{
				if (flag == true)
				{
					break;
				}
				String path = null;
				List<DynamicProperty> dps = oc.getPropSet();
				if (dps != null)
				{
					for (DynamicProperty dp : dps)
					{
						path = dp.getName();
						if (path.equalsIgnoreCase("name"))
						{
							String value = (String) dp.getVal();
							if (value.equals(VmName))
							{
								flag = true;
							}
						}
						if (path.equalsIgnoreCase("summary.runtime.host"))
						{
							mor = (ManagedObjectReference) dp.getVal();
							if (flag == true)
							{
								break;
							}
						}
					}
				}
			}
		}
然后根据这个(HostSystem)的ManagedObjectReference对象获取对应的主机的名称:

	private static String getObjectName(ManagedObjectReference mor) throws Exception
	{
		String objectName = null;
		
		PropertySpec propSpec = new PropertySpec();
		propSpec.setAll(new Boolean(false));
		propSpec.getPathSet().add("name");
		propSpec.setType(mor.getType());

		ObjectSpec objSpec = new ObjectSpec();
		objSpec.setObj(mor);
		objSpec.setSkip(new Boolean(false));

		PropertyFilterSpec spec = new PropertyFilterSpec();
		spec.getPropSet().add(propSpec);
		spec.getObjectSet().add(objSpec);
		
		ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>();
		listpfs.add(spec);
		List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
		if(listobjcont != null)
		{
			ObjectContent oc = (ObjectContent) listobjcont.get(0);
			objectName = (String) oc.getPropSet().get(0).getVal();
		}
		
		return objectName;
	}
根据主机的名称分别获得对应的物理适配器的Map<"mac地址",“网卡名称”>和vSwitch的Map<"mac地址",“ip地址”>的信息,然后遍历获得具有相同mac地址的信息,那个mac地址对应的ip地址就是主机地址:

	private static Map<String,String> getHostPhyIpByHostName(String hostName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
	{
		Map<String,String> map = new HashMap<String,String>();
        RetrieveResult propsHost = getRetrieveResultObjectWithProperty("HostSystem", "config.network.pnic");
        List<PhysicalNic> listpnic = null;
        if(propsHost != null)
        {
            Boolean flagpnic = false;
            for (ObjectContent oc : propsHost.getObjects())
            {
                if (flagpnic == true)
                {
                    break;
                }
                String path = null;
                List<DynamicProperty> dps = oc.getPropSet();

                if (dps != null)
                {
                    for (DynamicProperty dp : dps)
                    {
                        path = dp.getName();
                        if (path.equalsIgnoreCase("config.network.pnic"))
                        {
                            listpnic = ((ArrayOfPhysicalNic)dp.getVal()).getPhysicalNic();
                        }
                        if (path.equalsIgnoreCase("name"))
                        {
                            String value = (String) dp.getVal();
                            if (value.equals(hostName))
                            {
                                flagpnic = true;
                                break;
                            }
                        }
                    }
                }
            }
            
        }
        
        if(listpnic != null)
        {
            for(PhysicalNic pnic : listpnic)
            {
                PhysicalNicSpec pns = pnic.getSpec();
                String mac = pnic.getMac();
                if(pns != null)
                {
                    HostIpConfig hic = pns.getIp();
                    if(hic != null)
                    {
                        String ipAddress = hic.getIpAddress();
                        map.put(mac, ipAddress);
                    }
                }
            }
        }
        
        return map;
	}
	private static Map<String,String> getHostVirIpByHostName(String hostName) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg
	{
		Map<String,String> map = new HashMap<String,String>();
        RetrieveResult propsHost = getRetrieveResultObjectWithProperty("HostSystem", "config.network.vnic");
        List<HostVirtualNic> listpnic = null;
        if(propsHost != null)
        {
            Boolean flagpnic = false;
            for (ObjectContent oc : propsHost.getObjects())
            {
                if (flagpnic == true)
                {
                    break;
                }
                String path = null;
                List<DynamicProperty> dps = oc.getPropSet();

                if (dps != null)
                {
                    for (DynamicProperty dp : dps)
                    {
                        path = dp.getName();
                        if (path.equalsIgnoreCase("config.network.vnic"))
                        {
                            listpnic = ((ArrayOfHostVirtualNic)dp.getVal()).getHostVirtualNic();
                        }
                        if (path.equalsIgnoreCase("name"))
                        {
                            String value = (String) dp.getVal();
                            if (value.equals(hostName))
                            {
                                flagpnic = true;
                                break;
                            }
                        }
                    }
                }
            }
            
        }
        
        if(listpnic != null)
        {
            for(HostVirtualNic pnic : listpnic)
            {
            	HostVirtualNicSpec pns = pnic.getSpec();
                if(pns != null)
                {
                    HostIpConfig hic = pns.getIp();
                    String mac = pns.getMac();
                    if(hic != null)
                    {
                        String ipAddress = hic.getIpAddress();
                        map.put(mac, ipAddress);
                    }
                }
            }
        }
        
        return map;
	}
最后展示主要的方法(完整版):

	public static String getVmHostIpByVmName(String VmName) throws Exception
	{
		List<String> ret = new ArrayList<String>();
		RetrieveResult props = getRetrieveResultObjectWithProperty("VirtualMachine", "summary.runtime.host");
		ManagedObjectReference mor = null;
		if (props != null)
		{
			Boolean flag = false;
			for (ObjectContent oc : props.getObjects())
			{
				if (flag == true)
				{
					break;
				}
				String path = null;
				List<DynamicProperty> dps = oc.getPropSet();
				if (dps != null)
				{
					for (DynamicProperty dp : dps)
					{
						path = dp.getName();
						if (path.equalsIgnoreCase("name"))
						{
							String value = (String) dp.getVal();
							if (value.equals(VmName))
							{
								flag = true;
							}
						}
						if (path.equalsIgnoreCase("summary.runtime.host"))
						{
							mor = (ManagedObjectReference) dp.getVal();
							if (flag == true)
							{
								break;
							}
						}
					}
				}
			}
		}

		String hostName = null;
		if (mor != null)
		{
			hostName = getObjectName(mor);
		}
		
		if(hostName != null)
		{
			Map<String,String> phyMap = getHostPhyIpByHostName(hostName);
			Map<String,String> virMap = getHostVirIpByHostName(hostName);
			for(Map.Entry<String, String> entry : phyMap.entrySet())
			{
				String phyMac = entry.getKey();
				for(Map.Entry<String, String> entryvir : virMap.entrySet())
				{
					String virMac = entryvir.getKey();
					if(phyMac.equalsIgnoreCase(virMac))
					{
						ret.add(entryvir.getValue());
					}
				}
			}
		}
		
		String ipAddress = ret.get(0);
		
		return ipAddress;
	}


目录
相关文章
|
2月前
|
虚拟化
vmware克隆虚拟机后没有ip地址的问题
解决vmware克隆虚拟机后没有内网ip的问题
|
3月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Force IP强制修改网口IP功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Force IP强制修改网口IP功能(C++)
29 0
|
3月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用ForceIP强制修改网口IP功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用ForceIP强制修改网口IP功能(C#)
26 0
|
9天前
|
Linux 虚拟化
VMware workstation 中centos7虚拟机在nat模式下怎么配置网卡,指定我想要的IP并且可以联网
https://blog.csdn.net/2302_78534730/article/details/132825156?spm=1001.2014.3001.5502
131 0
|
2月前
|
网络协议 Linux 虚拟化
修改虚拟机ip为静态ip
centos修改ip为静态ip
|
3月前
|
Linux 虚拟化
Linux虚拟机不显示IP地址的解决办法
Linux虚拟机不显示IP地址的解决办法
90 0
|
3月前
|
网络协议 Linux Windows
Linux虚拟机设置固定IP
Linux虚拟机设置固定IP
39 2
|
5月前
|
API 网络架构
虚拟机绑定浮动ip只能选择同一个路由器上的吗
在虚拟网络环境中,绑定浮动IP通常是与路由器相关的操作。虚拟机绑定浮动IP时,通常是通过路由器将浮动IP映射到虚拟机的内部IP地址。 在一些云平台中,浮动IP地址通常与特定的路由器相关联。因此,如果你想要将浮动IP绑定到虚拟机,通常需要选择与该虚拟机所在的VPC(虚拟私有云)或子网相关联的路由器。 具体来说,通常有以下步骤: 1. **选择浮动IP:** 在云平台的控制台或使用相关API时,选择要绑定的浮动IP。 2. **选择路由器:** 在绑定浮动IP时,通常需要选择一个路由器,将浮动IP与该路由器相关联。 3. **选择虚拟机:** 然后,你需要选择要将浮动IP绑定到的虚拟机。
622 1
|
5月前
|
Linux 开发工具
Linux调整虚拟机ip地址
Linux调整虚拟机ip地址
129 0
|
5月前
|
Linux 开发工具
Linux调整虚拟机ip地址
Linux调整虚拟机ip地址
51 1