Java hundred percent gain to machine IP address and Mac code

Source: Internet
Author: User
Tags getmessage

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

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.