JAVA获得网卡号

原创 2005年05月27日 19:41:00

I've written some code to determine the MAC address of a network adapter. This question has been floating around for years, so maybe we can come up with a solution that will work in at least some environments !

The following issues are open from my side:
- Will probably not run in applets
- Tested Windows / Linux only
- Tested J2SDK 1.4 only
- If a computer has more than one network adapters, only one MAC address will be returned
- will not run if user does not have permissions to run ifconfig/ipconfig (e.g. under linux this is typically only permitted for root)

Any comments are highly appreciated.
Holger


/** try to determine MAC address of local network card; this is done
    using a shell to run ifconfig (linux) or ipconfig (windows). The
    output of the processes will be parsed.

    <p>

    To run the whole thing, just type java NetworkInfo

    <p>

    Current restrictions:

    <ul>
       <li>Will probably not run in applets

       <li>Tested Windows / Linux only

       <li>Tested J2SDK 1.4 only

       <li>If a computer has more than one network adapters, only
           one MAC address will be returned

       <li>will not run if user does not have permissions to run
           ifconfig/ipconfig (e.g. under linux this is typically
           only permitted for root)
    </ul>

  */
 
import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
 
public final class NetworkInfo {
 private final static String getMacAddress() throws IOException {
  String os = System.getProperty("os.name");
 
  try {
   if(os.startsWith("Windows")) {
    return windowsParseMacAddress(windowsRunIpConfigCommand());
   } else if(os.startsWith("Linux")) {
    return linuxParseMacAddress(linuxRunIfConfigCommand());
   } else {
    throw new IOException("unknown operating system: " + os);
   }
  } catch(ParseException ex) {
   ex.printStackTrace();
   throw new IOException(ex.getMessage());
  }
 }
 
 
 /*
  * Linux stuff
  */
 private final static String linuxParseMacAddress(String ipConfigResponse) throws ParseException {
  String localHost = null;
  try {
   localHost = InetAddress.getLocalHost().getHostAddress();
  } catch(java.net.UnknownHostException ex) {
   ex.printStackTrace();
   throw new ParseException(ex.getMessage(), 0);
  }
 
  StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "/n");
  String lastMacAddress = null;
 
  while(tokenizer.hasMoreTokens()) {
   String line = tokenizer.nextToken().trim();
   boolean containsLocalHost = line.indexOf(localHost) >= 0;
 
   // see if line contains IP address
   if(containsLocalHost && lastMacAddress != null) {
    return lastMacAddress;
   }
 
   // see if line contains MAC address
   int macAddressPosition = line.indexOf("HWaddr");
   if(macAddressPosition <= 0) continue;
 
   String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
   if(linuxIsMacAddress(macAddressCandidate)) {
    lastMacAddress = macAddressCandidate;
    continue;
   }
  }
 
  ParseException ex = new ParseException
   ("cannot read MAC address for " + localHost + " from [" + ipConfigResponse + "]", 0);
  ex.printStackTrace();
  throw ex;
 }
 
 
 private final static boolean linuxIsMacAddress(String macAddressCandidate) {
  // TODO: use a smart regular expression
  if(macAddressCandidate.length() != 17) return false;
  return true;
 }
 
 
 private final static String linuxRunIfConfigCommand() throws IOException {
  Process p = Runtime.getRuntime().exec("ifconfig");
  InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
 
  StringBuffer buffer= new StringBuffer();
  for (;;) {
   int c = stdoutStream.read();
   if (c == -1) break;
   buffer.append((char)c);
  }
  String outputText = buffer.toString();
 
  stdoutStream.close();
 
  return outputText;
 }
 
 
 
 /*
  * Windows stuff
  */
 private final static String windowsParseMacAddress(String ipConfigResponse) throws ParseException {
  String localHost = null;
  try {
   localHost = InetAddress.getLocalHost().getHostAddress();
  } catch(java.net.UnknownHostException ex) {
   ex.printStackTrace();
   throw new ParseException(ex.getMessage(), 0);
  }
 
  StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "/n");
  String lastMacAddress = null;
 
  while(tokenizer.hasMoreTokens()) {
   String line = tokenizer.nextToken().trim();
 
   // see if line contains IP address
   if(line.endsWith(localHost) && lastMacAddress != null) {
    return lastMacAddress;
   }
 
   // see if line contains MAC address
   int macAddressPosition = line.indexOf(":");
   if(macAddressPosition <= 0) continue;
 
   String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
   if(windowsIsMacAddress(macAddressCandidate)) {
    lastMacAddress = macAddressCandidate;
    continue;
   }
  }
 
  ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigResponse + "]", 0);
  ex.printStackTrace();
  throw ex;
 }
 
 
 private final static boolean windowsIsMacAddress(String macAddressCandidate) {
  // TODO: use a smart regular expression
  if(macAddressCandidate.length() != 17) return false;
 
  return true;
 }
 
 
 private final static String windowsRunIpConfigCommand() throws IOException {
  Process p = Runtime.getRuntime().exec("ipconfig /all");
  InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
 
  StringBuffer buffer= new StringBuffer();
  for (;;) {
   int c = stdoutStream.read();
   if (c == -1) break;
   buffer.append((char)c);
  }
  String outputText = buffer.toString();
 
  stdoutStream.close();
 
  return outputText;
 }
 
 
 
 /*
  * Main
  */
 public final static void main(String[] args) {
  try {
   System.out.println("Network infos");
 
   System.out.println("  Operating System: " + System.getProperty("os.name"));
   System.out.println("  IP/Localhost: " + InetAddress.getLocalHost().getHostAddress());
   System.out.println("  MAC Address: " + getMacAddress());
  } catch(Throwable t) {
   t.printStackTrace();
  }
 }
}

java实现银行卡卡号 Luhm 校验算法

转自http://blog.csdn.net/chao121/article/details/5870932 现行 16 位银联卡现行卡号开头 6 位是 622126~622925 之间的,7 到...
  • linbrain0000
  • linbrain0000
  • 2015年11月15日 10:45
  • 3586

Java实现Luhm算法--银行卡号合法性校验

银行卡是由”发卡行标识代码 + 自定义 + 校验码 “等部分组成的。 银联标准卡与以往发行的银行卡最直接的区别就是其卡号前6位数字的不同。   银行卡卡号的前6位是用来表示发卡银行或机构的,称为“发卡...
  • archer119
  • archer119
  • 2016年07月31日 21:26
  • 2796

Java 根据银行卡号获取银行名称以及图标

参考 CNBankCard 中国各大银行卡号查询 一、pom.xml 一、支付宝接口获取名称 1.根据 卡号 获取 银行信息 接口 https://ccdcapi.alipay.com/val...
  • N_007
  • N_007
  • 2017年12月18日 17:54
  • 227

JAVA在linux和windows下获得网卡号

 import java.net.InetAddress;import java.io.InputStream;import java.io.BufferedInputStream;import ja...
  • love_tu
  • love_tu
  • 2008年11月10日 15:16
  • 403

八、java项目常用工具类之卡号,手机号隐藏工具类

一、问题描述及试用场景: 在项目开发中,对于一些敏感数据需要特殊处理;比如在落地数据库或打印日志时需要把卡号前6后4,手机号前3后4等; 二、样例源码: package org.egg....
  • hupoling
  • hupoling
  • 2017年11月12日 15:50
  • 291

Java实现银行卡号的校验j

核心校验函数: /* 校验过程: 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位...
  • rabbit_ding0810
  • rabbit_ding0810
  • 2016年12月09日 17:40
  • 2717

银行卡号格式化带星显示

+ (NSString *)formateBankNum:(NSString *)cardId { NSUInteger lenth = cardId.length; NSString...
  • shan1991fei
  • shan1991fei
  • 2016年12月13日 13:43
  • 1147

java 根据银行卡号来获取银行名称

原文地址:http://blog.csdn.net/a1106900429/article/details/48375031/** * 通过银行的Bin号 来获取 银行名称 * @auth...
  • qq_26761229
  • qq_26761229
  • 2016年11月21日 18:25
  • 3900

通过银行卡号获取银行名称

根据银行卡号找银行名称
  • fighterandknight
  • fighterandknight
  • 2016年06月12日 10:17
  • 7275

C#中得到网卡号

C#中得到网卡号 using System.Management;//get the MAC addressManagementClass mc=new ManagementClass("Win32_...
  • ahshow
  • ahshow
  • 2007年08月28日 10:26
  • 618
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAVA获得网卡号
举报原因:
原因补充:

(最多只允许输入30个字)