java获取网卡正真的mac_Java 实现读取本机网卡Mac地址

本方法主要用来限制系统在其他的机器上运行.其实原理简单的很.没有调用第三方插件.代码如下:

package com.users.util;

import    java.io.*;

import    java.util.*;

import    java.util.regex.*;

public    class    NetID    {

String    IPCONFIG_COMMAND_WIN    =    "ipconfig    /all";

boolean    realMac    =    true;

String    unique    =    "";

public    static    String    getMacAddress()    {

NetID    hwid    =    new    NetID();

return    hwid.getUnique().trim();

}

private    String    getUnique()    {

String    os    =    System.getProperty("os.name");

if    (os.startsWith("Windows"))    {

return    getUniqueWindows();

}else    {

return    "";

}

}

private    String    getUniqueWindows()    {

String    ipConfigResponse    =    null;

try    {

ipConfigResponse    =    runConsoleCommand(IPCONFIG_COMMAND_WIN);

}

catch    (IOException    e)    {

e.printStackTrace();

}

StringTokenizer    tokenizer    =    new    StringTokenizer(ipConfigResponse,    "\n");

while    (tokenizer.hasMoreTokens())    {

String    line    =    tokenizer.nextToken().trim();

int    macAddressPosition    =    line.indexOf(":");

if    (macAddressPosition    <=    0)    {

continue;

}

String    macAddressCandidate    =    line.substring(macAddressPosition    +    1).

trim();

if    (isMacAddWin(macAddressCandidate))    {

if    (realMac    ==    true)    {

generateUnique(macAddressCandidate);

}

else    {

realMac    =    true;

}

}

}

return    unique;

}

private    String    runConsoleCommand(String    command)    throws    IOException    {

Process    p    =    Runtime.getRuntime().exec(command);

InputStream    stdoutStream    =    new    BufferedInputStream(p.getInputStream());

StringBuffer    buffer    =    new    StringBuffer();

while    (true)    {

int    c    =    stdoutStream.read();

if    (c    ==    -1)    {

break;

}

buffer.append(    (char)    c);

}

String    outputText    =    buffer.toString();

stdoutStream.close();

return    outputText;

}

private    boolean    isMacAddWin(String    macAddressCandidate)    {

Pattern    macPattern    =

Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");

Matcher    m    =    macPattern.matcher(macAddressCandidate);

return    m.matches();

}

private    boolean    isMacAddOSX(String    macAddressCandidate)    {

if    (macAddressCandidate.length()    !=    17)    {

return    false;

}

else    {

return    true;

}

}

private    void    generateUnique(String    macAddress)    {

if    (unique    ==    "")    {

unique    +=    macAddress;

}

else    {

unique    +=    "#";

unique    +=    macAddress;

}

}

public    static    void    main(String [] args)    {

System.out.println(NetID.getMacAddress());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值