javaプログラミングは、サーバのIPアドレスとMACアドレスを取得する方法を実現する。


本明細書の例は、JavaプログラミングがサーバIPアドレスおよびMACアドレスを取得する方法を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
測定済みシステム:
windows linux unix
127.1.0.1や0.0.0.1などの非正常IPを除外します。

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class IpUtil {
 private IpUtil(){}
 /**
  *        :      IP  
  * @author: [email protected]
  * @version: 2014 9 5    4:57:15
  */
 public static String getLocalIP() {
  String sIP = "";
  InetAddress ip = null;
  try {
   boolean bFindIP = false;
   Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) 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) {
   OutUtil.error(IpUtil.class, e.getMessage());
  }
  if (null != ip) {
   sIP = ip.getHostAddress();
  }
  return sIP;
 }
 /**
  *        :      IP  (   )
  * @author: [email protected]
  * @version: 2014 9 5    4:57:15
  */
 public static List<String> getLocalIPS() {
  InetAddress ip = null;
  List<String> ipList = new ArrayList<String>();
  try {
   Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
     .getNetworkInterfaces();
   while (netInterfaces.hasMoreElements()) {
    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}")) {
      ipList.add(ip.getHostAddress());
     }
    }
   }
  } catch (Exception e) {
   OutUtil.error(IpUtil.class, e.getMessage());
  }
  return ipList;
 }
 /**
  *        :      MAC  
  * @author: [email protected]
  * @version: 2014 9 5    1:27:25
  */
 public static String getMacId() {
  String macId = "";
  InetAddress ip = null;
  NetworkInterface ni = null;
  try {
   boolean bFindIP = false;
   Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
     .getNetworkInterfaces();
   while (netInterfaces.hasMoreElements()) {
    if (bFindIP) {
     break;
    }
    ni = (NetworkInterface) netInterfaces
      .nextElement();
    // ----------    ,     ni.getName  
    //     ip
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
     ip = (InetAddress) ips.nextElement();
     if (!ip.isLoopbackAddress() //  127.0.0.1
       && ip.getHostAddress().matches(
         "(\\d{1,3}\\.){3}\\d{1,3}")) {
      bFindIP = true;
      break;
     }
    }
   }
  } catch (Exception e) {
   OutUtil.error(IpUtil.class, e.getMessage());
  }
  if (null != ip) {
   try {
    macId = getMacFromBytes(ni.getHardwareAddress());
   } catch (SocketException e) {
    OutUtil.error(IpUtil.class, e.getMessage());
   }
  }
  return macId;
 }
 /**
  *        :      MAC  (   )
  * @author: [email protected]
  * @version: 2014 9 5    1:27:25
  */
 public static List<String> getMacIds() {
  InetAddress ip = null;
  NetworkInterface ni = null;
  List<String> macList = new ArrayList<String>();
  try {
   Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
     .getNetworkInterfaces();
   while (netInterfaces.hasMoreElements()) {
    ni = (NetworkInterface) netInterfaces
      .nextElement();
    // ----------    ,     ni.getName  
    //     ip
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
     ip = (InetAddress) ips.nextElement();
     if (!ip.isLoopbackAddress() //  127.0.0.1
       && ip.getHostAddress().matches(
         "(\\d{1,3}\\.){3}\\d{1,3}")) {
      macList.add(getMacFromBytes(ni.getHardwareAddress()));
     }
    }
   }
  } catch (Exception e) {
   OutUtil.error(IpUtil.class, e.getMessage());
  }
  return macList;
 }
 private static String getMacFromBytes(byte[] bytes) {
  StringBuffer mac = new StringBuffer();
  byte currentByte;
  boolean first = false;
  for (byte b : bytes) {
   if (first) {
    mac.append("-");
   }
   currentByte = (byte) ((b & 240) >> 4);
   mac.append(Integer.toHexString(currentByte));
   currentByte = (byte) (b & 15);
   mac.append(Integer.toHexString(currentByte));
   first = true;
  }
  return mac.toString().toUpperCase();
 }
}
本論文で述べたように、皆さんのJavaプログラムの設計に役に立ちます。