Vijava Learning Notes (Virtualmachine get NIC configuration information)

Source: Internet
Author: User

Source:

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;
 /** * Virtual Organization network card * Created by vixuan-008 on 2015/6/8. */public class Virtualmachinenetcardconfig {public static void main (string[] args) {try{servicei
        Nstance 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 ();
                Virtual Organization 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 ());
                Custom NIC Information guestnicinfo[] Guestnicinfos=guestinfo.getnet (); if (guestnicinfos!=null && guestnicinfos.length>0) {for (Guestnicinfo guestnicinfo:guestnicin FOS) {System.out.println (guestnicinfo.getmacaddress ());//mac address System.out . println (Guestnicinfo.getdeviceconfigid ());//deviceconfigid System.out.println (guestnicinfo.getnet
        Work ());//Network Group                String[] Ipaddress=guestnicinfo.getipaddress ();
                                if (ipaddress!=null && ipaddress.length>0) {for (String address:ipaddress) {
                                if (isIp (address)) {System.out.println;//IP addresses  }}} Netdnsconfiginfo
                        Netdnsconfiginfo=guestnicinfo.getdnsconfig ();
                       String[] Dnsaddress=netdnsconfiginfo.getipaddress ();
                               if (dnsaddress!=null && dnsaddress.length>0) {for (String address:dnsaddress) {
                               if (isIp (address)) {System.out.println;//dns addresses


            }
                           }
                       }
                    }
                } }}catch (excEption e) {e.printstacktrace ();
        Return }///Determine if the IP address is legitimate public static Boolean isIp (String addr) {if (Addr.length () < 7 | | | addr.length ( ) > 15 | |
        "". Equals (addr)) {return false; /** * To determine the IP format and scope */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;
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.