linux上使用java获取本机IP地址和MAC地址

来源:互联网 发布:qq飞车紫电神驹数据 编辑:程序博客网 时间:2024/04/28 00:09

网上很多用java获取本机IP和本机MAC地址的代码,但是根据不同的操作系统,并不是都有用.

本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过

(注意:如果有多块网卡,可能出错)

下面给出代码:

import java.net.*;
import java.util.*;

public class Test {
 public static void main(String[] args) {
  
  Test t = new Test();
  System.out.println(t.getLocalIP());
  System.out.println(t.getMacAddr());
 }

 public String getMacAddr() {
  String MacAddr = "";
  String str = "";
  try {
   NetworkInterface NIC = NetworkInterface.getByName("eth0");
   byte[] buf = NIC.getHardwareAddress();
   for (int i = 0; i < buf.length; i++) {
    str = str + byteHEX(buf[i]);
   }
   MacAddr = str.toUpperCase();
  } catch (SocketException e) {
   e.printStackTrace();
   System.exit(-1);
  }
  return MacAddr;
 }

 public String getLocalIP() {
  String ip = "";
  try {
   Enumeration<?> e1 = (Enumeration<?>) NetworkInterface
     .getNetworkInterfaces();
   while (e1.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) e1.nextElement();
    if (!ni.getName().equals("eth0")) {
     continue;
    } else {
     Enumeration<?> e2 = ni.getInetAddresses();
     while (e2.hasMoreElements()) {
      InetAddress ia = (InetAddress) e2.nextElement();
      if (ia instanceof Inet6Address)
       continue; 
      ip = ia.getHostAddress();
     }
     break;
    }
   }
  } catch (SocketException e) {
   e.printStackTrace();
   System.exit(-1);
  }
  return ip;
 }

 /* 一个将字节转化为十六进制ASSIC码的函数 */
 public static String byteHEX(byte ib) {
  char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
    'b', 'c', 'd', 'e', 'f' };
  char[] ob = new char[2];
  ob[0] = Digit[(ib >>> 4) & 0X0F];
  ob[1] = Digit[ib & 0X0F];
  String s = new String(ob);
  return s;
 }
}

原创粉丝点击