开发者社区> 朱小厮> 正文

使用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;
	}


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
VMWare开启虚拟机报错:此主机支持Intel VT-x, 但Intel VT-x处于禁用状态
VMWare开启虚拟机报错:此主机支持Intel VT-x, 但Intel VT-x处于禁用状态
10 0
禁用vmware虚拟机自动挂起功能
禁用vmware虚拟机自动挂起功能
16 0
VMware Workstation 在此主机上不支持嵌套虚拟化。模块“MonitorMode”启动失败。未能启动虚拟机。
VMware Workstation 在此主机上不支持嵌套虚拟化。模块“MonitorMode”启动失败。未能启动虚拟机。
41 0
vmware虚拟机挂起后无ip,无法ping外网
vmware虚拟机挂起后无ip,无法ping外网
18 0
解决“虚拟机使用的是此版本 VMware Workstation 不支持的硬件版本”的问题。
解决“虚拟机使用的是此版本 VMware Workstation 不支持的硬件版本”的问题。
101 0
VMware 虚拟机 Ubuntu 系统执行 ifconfig 命令 eth0没有IP地址(intet addr、Bcast、Mask)
VMware 虚拟机 Ubuntu 系统执行 ifconfig 命令 eth0没有IP地址(intet addr、Bcast、Mask)
36 0
别用 VMware 了,这款虚拟机简单、轻量、好用还免费...
别用 VMware 了,这款虚拟机简单、轻量、好用还免费...
71 0
VMware Linux虚拟机与WIN7操作系统共享无线网络上网配置
VMware Linux虚拟机与WIN7操作系统共享无线网络上网配置
28 0
VMware 创建VMware9虚拟机及设置详细教程
VMware 创建VMware9虚拟机及设置详细教程
28 0
网络基础 利用vnc viewer访问在vmware虚拟机上的linux
网络基础 利用vnc viewer访问在vmware虚拟机上的linux
44 0
+关注
朱小厮
主要从事消息中间件的相关研发工作,著有《RabbitMQ实战指南》。
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
一个跨平台的云服务SDK需要什么
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多