Vijava 学习笔记之(获取自定义规范定义的IP相关配置信息)
阿里
阅读:693
2021-03-31 22:29:34
评论:0
源代码:
package com.vmware.customzation;
import com.vmware.util.Session;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by vixuan-008 on 2015/6/1.
*/
public class UserCustomzation {
public static void main(String[] args){
ServiceInstance serviceInstance=null;
try{
serviceInstance= Session.getInstance("192.168.0.22", "administrator@vsphere.local", "Vixuan12#");
Map map=new HashMap();
com.vmware.vim25.mo.CustomizationSpecManager manager=serviceInstance.getCustomizationSpecManager();
//获取自定义规范
getNetcard(serviceInstance, "windows7");
serviceInstance.getServerConnection().logout();
}catch (Exception e){
e.printStackTrace();
serviceInstance.getServerConnection().logout();
return;
}
}
//查询用户已经创建的自定义规范
public static CustomizationSpec getCustomizationSpec( ServiceInstance instance,String customizationName){
CustomizationSpec customizationSpec=null;
CustomizationSpecItem customizationSpecItem=null;
com.vmware.vim25.mo.CustomizationSpecManager manager=instance.getCustomizationSpecManager();
CustomizationSpecInfo[] infos=manager.getInfo();
if(infos!=null && infos.length>0){
for(CustomizationSpecInfo info:infos){
if(info.getName().equalsIgnoreCase(customizationName)){
try{
customizationSpecItem=manager.getCustomizationSpec(customizationName);
customizationSpec=customizationSpecItem.getSpec();
}catch (Exception e){
e.printStackTrace();
return customizationSpec;
}
}
}
}
return customizationSpec;
}
public static List getNetcard(ServiceInstance instance,String customizationName){
List<Object> list=new ArrayList<Object>();
CustomizationSpec customizationSpec=null;
CustomizationSpecItem customizationSpecItem=null;
com.vmware.vim25.mo.CustomizationSpecManager manager=instance.getCustomizationSpecManager();
CustomizationSpecInfo[] infos=manager.getInfo();
if(infos!=null && infos.length>0){
for(CustomizationSpecInfo info:infos){
if(info.getName().equalsIgnoreCase(customizationName)){
try{
customizationSpecItem=manager.getCustomizationSpec(customizationName);
customizationSpec=customizationSpecItem.getSpec();
CustomizationAdapterMapping[] adapterMappings=customizationSpec.nicSettingMap;
if(adapterMappings!=null && adapterMappings.length>0){
for(CustomizationAdapterMapping adapter:adapterMappings){
CustomizationIPSettings ipSettings=adapter.adapter;
//IP地址疑惑
CustomizationFixedIp fixedIp=(CustomizationFixedIp)ipSettings.ip;
System.out.println(fixedIp.ipAddress);//IP地址
System.out.println(ipSettings.subnetMask);//subnetMask
String[] gateways=ipSettings.gateway;
if(gateways!=null && gateways.length>0){
for(String str:gateways){
if(!str.equalsIgnoreCase("")){
System.out.println(str);//gateway
}
}
}
String[] dnss=ipSettings.dnsServerList;
if(dnss!=null && dnss.length>0){
for(String dns:dnss){
System.out.println(dns); //dnsServer
}
}
}
}
}catch (Exception e){
e.printStackTrace();
return list;
}
}
}
}
return list;
}
}
效果截图:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。