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;
}
}