源代码:
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;
}
}
}