您的位置:首页 > 编程语言 > Java开发

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

2015-06-08 14:51 387 查看
源代码:

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;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: