ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importjava.net.InetAddress;ImportJava.net.NetworkInterface;Importjava.net.SocketException;Importjava.util.ArrayList;Importjava.util.Enumeration;Importjava.util.List; Public classIputil {Private Static FinalLogger Logger = Loggerfactory.getlogger (iputil.class); PrivateIputil () {}/*** This method describes: Get the IP address of the server*/ Public Staticstring Getlocalip () {string SIP= ""; InetAddress IP=NULL; Try { BooleanBfindip =false; Enumeration<NetworkInterface> netinterfaces =networkinterface.getnetworkinterfaces (); while(Netinterfaces.hasmoreelements ()) {if(BFINDIP) { Break; } networkinterface ni=(NetworkInterface) netinterfaces. Nextelement (); Enumeration<InetAddress> ips =ni.getinetaddresses (); while(Ips.hasmoreelements ()) {IP=(inetaddress) ips.nextelement (); if(!ip.isloopbackaddress ()&&ip.gethostaddress (). Matches ("(\\d{1,3}\\.) {3}\\d{1,3} ") ) {Bfindip=true; Break; } } } } Catch(Exception e) {logger.error ("Error obtaining IP address of server: {}", E.getmessage ()); } if(NULL!=IP) {SIP=ip.gethostaddress (); } returnSIP; } /*** This method describes: Get the IP address of the server (multi-NIC)*/ Public StaticList<string>getlocalips () {inetaddress IP=NULL; List<String> IPList =NewArraylist<string>(); Try{Enumeration<NetworkInterface> netinterfaces =networkinterface.getnetworkinterfaces (); while(Netinterfaces.hasmoreelements ()) {NetworkInterface ni=(NetworkInterface) netinterfaces. Nextelement (); Enumeration<InetAddress> ips =ni.getinetaddresses (); while(Ips.hasmoreelements ()) {IP=ips.nextelement (); if(!ip.isloopbackaddress ()&&ip.gethostaddress (). Matches ("(\\d{1,3}\\.) {3}\\d{1,3} ") {Iplist.add (ip.gethostaddress ()); } } } } Catch(Exception e) {logger.error ("Error getting the IP address of the server (multiple NIC): {}", E.getmessage ()); } returnIPList; } /*** This method describes: get the MAC address of the server*/ Public Staticstring Getmacid () {string MacId= ""; InetAddress IP=NULL; NetworkInterface ni=NULL; Try { BooleanBfindip =false; Enumeration<NetworkInterface> netinterfaces =networkinterface.getnetworkinterfaces (); while(Netinterfaces.hasmoreelements ()) {if(BFINDIP) { Break; } ni=netinterfaces. nextelement (); //----------specific situations, consider using Ni.getname to judge//Traverse all IPsenumeration<inetaddress> ips =ni.getinetaddresses (); while(Ips.hasmoreelements ()) {IP=ips.nextelement (); if(!ip.isloopbackaddress ()//Non-127.0.0.1&&ip.gethostaddress (). Matches ("(\\d{1,3}\\.) {3}\\d{1,3} ") ) {Bfindip=true; Break; } } } } Catch(Exception e) {logger.error ("Error getting server's MAC address: {}", E.getmessage ()); } if(NULL!=IP) { Try{macId=getmacfrombytes (Ni.gethardwareaddress ()); } Catch(SocketException e) {logger.error ("Error getting server's MAC address: {}", E.getmessage ()); } } returnmacId; } /*** This method describes: Get the server's MAC address (multi-NIC)*/ Public StaticList<string>Getmacids () {inetaddress IP=NULL; NetworkInterface ni=NULL; List<String> maclist =NewArraylist<string>(); Try{Enumeration<NetworkInterface> netinterfaces =networkinterface.getnetworkinterfaces (); while(Netinterfaces.hasmoreelements ()) {NI=netinterfaces. nextelement (); //----------specific situations, consider using Ni.getname to judge//Traverse all IPsenumeration<inetaddress> ips =ni.getinetaddresses (); while(Ips.hasmoreelements ()) {IP=ips.nextelement (); if(!ip.isloopbackaddress ()//Non-127.0.0.1&&ip.gethostaddress (). Matches ("(\\d{1,3}\\.) {3}\\d{1,3} ") {maclist.add (Getmacfrombytes (Ni.gethardwareaddress ())); } } } } Catch(Exception e) {logger.error ("Error getting server's MAC address (multiple NIC): {}", E.getmessage ()); } returnmaclist; } Private StaticString Getmacfrombytes (byte[] bytes) {StringBuffer mac=NewStringBuffer (); byteCurrentbyte; BooleanFirst =false; for(byteb:bytes) { if(first) {Mac.append ("-"); } currentbyte= (byte) ((b & c) >> 4); Mac.append (integer.tohexstring (currentbyte)); Currentbyte= (byte) (B & 15); Mac.append (integer.tohexstring (currentbyte)); First=true; } returnmac.tostring (). toUpperCase (); } Public Static voidMain (string[] args) {String localip=Iputil.getlocalip (); List<String> localips =iputil.getlocalips (); String macId=iputil.getmacid (); List<String> Macids =Iputil.getmacids (); System.out.println (Localip); System.out.println (localips); System.out.println (MACID); System.out.println (Macids); }}
Java hundred percent gain to machine IP address and Mac code