Vijava 学习笔记之(VirtualMachine 获取网卡配置信息)分析

符号 阅读:226 2021-03-31 22:29:51 评论: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.NetDnsConfigInfo; 
import com.vmware.vim25.mo.*; 
 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
/** 
 * 虚拟机关联网卡 
 * 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", "java"); 
            if (mes == null) { 
                serviceInstance.getServerConnection().logout(); 
                throw new Exception(); 
            } else { 
                VirtualMachine virtualMachine = (VirtualMachine) mes; 
                GuestInfo guestInfo=virtualMachine.getGuest(); 
                //虚拟机关联主机 
                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()); 
                //自定义网卡信息 
                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 
                        System.out.println(guestNicInfo.getNetwork());//网络组 
                        String[] ipAddress=guestNicInfo.getIpAddress(); 
                        if(ipAddress!=null && ipAddress.length>0){ 
                            for(String address:ipAddress){ 
                                if(isIp(address)){ 
                                    System.out.println(address);//Ip 地址 
                                } 
                            } 
                        } 
                        NetDnsConfigInfo netDnsConfigInfo=guestNicInfo.getDnsConfig(); 
                        String[] dnsAddress=netDnsConfigInfo.getIpAddress(); 
                       if(dnsAddress!=null && dnsAddress.length>0){ 
                           for(String address:dnsAddress){ 
                               if(isIp(address)){ 
                                   System.out.println(address);//dns 地址 
                               } 
                           } 
                       } 
                    } 
                } 
 
 
            } 
        }catch (Exception e){ 
            e.printStackTrace(); 
            return; 
        } 
    } 
    //判断IP地址是否合法 
    public static boolean isIp(String addr) 
    { 
        if(addr.length() < 7 || addr.length() > 15 || "".equals(addr)) 
        { 
            return false; 
        } 
        /** 
         * 判断IP格式和范围 
         */ 
        String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"; 
 
        Pattern pat = Pattern.compile(rexp); 
 
        Matcher mat = pat.matcher(addr); 
 
        boolean ipAddress = mat.find(); 
 
        return ipAddress; 
    } 
} 


声明

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

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号