Vijava 学习笔记之(VirtualMachine 与HostSystem 宿主关系)分析

java哥 阅读:169 2021-03-31 22:30:02 评论:0

源代码:

package com.vmware.client; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.GuestInfo; 
import com.vmware.vim25.GuestNicInfo; 
import com.vmware.vim25.ManagedObjectReference; 
import com.vmware.vim25.mo.*; 
 
/** 
 * 虚拟机关联网卡 
 * Created by vixuan-008 on 2015/6/8. 
 */ 
public class VirtualMachineNetCardConfig { 
    public static void main(String[] args){ 
        try{ 
           // ServiceInstance serviceInstance = Session.getInstance("****", "***", "***"); 
           ServiceInstance serviceInstance = Session.getInstance("***", "***", "***"); 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            ManagedEntity mes = new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", "AD"); 
            if (mes == null) { 
                serviceInstance.getServerConnection().logout(); 
                throw new Exception(); 
            } else { 
                VirtualMachine virtualMachine = (VirtualMachine) mes; 
                GuestInfo guestInfo=virtualMachine.getGuest(); 
                //系统类型 
                String os=guestInfo.getGuestFullName(); 
                //IP地址 
                String ipAddress=guestInfo.getIpAddress(); 
               <span style="color:#ff0000;"> //虚拟机关联主机 
                ManagedObjectReference mor = new ManagedObjectReference(); 
                mor.setType("HostSystem"); 
                mor.setVal(virtualMachine.getRuntime().getHost().getVal()); 
                HostSystem result =  new HostSystem(serviceInstance.getServerConnection(),mor); 
                System.out.println("hostname is:"+ result.getName());</span> 
                //自定义网卡信息 
                GuestNicInfo[] guestNicInfos=guestInfo.getNet(); 
                if(guestNicInfos!=null && guestNicInfos.length>0){ 
                    for(GuestNicInfo guestNicInfo:guestNicInfos){ 
                        System.out.println(guestNicInfo.getMacAddress());//mac 地址 
                        System.out.println(guestNicInfo.getDeviceConfigId());//deviceConfigId 
                    } 
                } 
 
 
            } 
        }catch (Exception e){ 
            e.printStackTrace(); 
            return; 
        } 
    } 
} 


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号