zoukankan      html  css  js  c++  java
  • java获取硬盘ID以及MAC地址

    http://blog.csdn.net/coolwzjcool/article/details/6698327 

    为了达到软件注册,或者说软件和电脑绑定的目的,需要将电脑上的固定编号进行一系列的算法计算,并生成唯一和软件匹配的号码。

    那么使用java如何达到这个目的呢?

    通常做法都是通过java的Runtime来完成,通过 process的输入流,进行获取相关的信息。

    下面列举具体的例子:

     1、DiskUtils 获取硬盘编号

    [java] view plain copy
     
    1. import java.io.File;  
    2. import java.io.FileWriter;  
    3. import java.io.BufferedReader;  
    4. import java.io.InputStreamReader;  
    5.   
    6. class DiskUtils {  
    7.     private DiskUtils() {  
    8.     }  
    9.   
    10.     public static String getSerialNumber(String drive) {  
    11.         String result = "";  
    12.         try {  
    13.             File file = File.createTempFile("damn", ".vbs");  
    14.             file.deleteOnExit();  
    15.             FileWriter fw = new java.io.FileWriter(file);  
    16.             String vbs = "Set objFSO = CreateObject("Scripting.FileSystemObject") "  
    17.                     + "Set colDrives = objFSO.Drives "  
    18.                     + "Set objDrive = colDrives.item(""  
    19.                     + drive  
    20.                     + "") "  
    21.                     + "Wscript.Echo objDrive.SerialNumber"; // see note  
    22.             fw.write(vbs);  
    23.             fw.close();  
    24.             Process p = Runtime.getRuntime().exec(  
    25.                     "cscript //NoLogo " + file.getPath());  
    26.             BufferedReader input = new BufferedReader(new InputStreamReader(  
    27.                     p.getInputStream()));  
    28.             String line;  
    29.             while ((line = input.readLine()) != null) {  
    30.                 result += line;  
    31.   
    32.             }  
    33.             input.close();  
    34.         } catch (Exception e) {  
    35.             e.printStackTrace();  
    36.         }  
    37.         return result.trim();  
    38.     }  
    39. }  

    2、MacUtils 获取MAC地址

    [java] view plain copy
     
    1. import java.io.InputStreamReader;  
    2. import java.io.LineNumberReader;  
    3.   
    4.   
    5. public class MacUtils {  
    6.   
    7.     public static void  getMac(){  
    8.     try {  
    9.   
    10.         Process process = Runtime.getRuntime().exec("ipconfig /all");  
    11.   
    12.         InputStreamReader ir = new InputStreamReader(process.getInputStream());  
    13.   
    14.         LineNumberReader input = new LineNumberReader(ir);  
    15.   
    16.         String line;  
    17.   
    18.         while ((line = input.readLine()) != null)  
    19.               
    20.   
    21.         if (line.indexOf("Physical Address") > 0) {  
    22.   
    23.             String MACAddr = line.substring(line.indexOf("-") - 2);  
    24.   
    25.             System.out.println("MAC address = [" + MACAddr + "]");  
    26.   
    27.         }  
    28.   
    29.         } catch (java.io.IOException e) {  
    30.   
    31.             System.err.println("IOException " + e.getMessage());  
    32.   
    33.         }  
    34.     }  
    35. }  


    3、 测试程序:

    [java] view plain copy
     
    1. import java.io.InputStreamReader;  
    2. import java.io.LineNumberReader;  
    3. import java.net.NetworkInterface;  
    4. import java.net.SocketException;  
    5. import java.util.Enumeration;  
    6. import java.util.Vector;  
    7.   
    8.   
    9. public class TestMain {  
    10.   
    11.     /** 
    12.      * @param args 
    13.      */  
    14.     public static void main(String[] args) {  
    15.         // TODO Auto-generated method stub  
    16.           
    17.         //****************获取MAC地址*****************//  
    18.         System.out.println("***MAC地址***");  
    19.         MacUtils.getMac();  
    20.         //****************获取硬盘ID*****************//  
    21.         String sn = DiskUtils.getSerialNumber("C");   
    22.         System.out.println("***硬盘编号***");  
    23.         System.out.println(sn);   
    24.   
    25.     }  
    26.   
    27. }  


    4、执行结果(我电脑上有几个VPN,所以就有多个MAC;为了防止别人搞我的电脑,数字和字母用*号代替)

    ***MAC地址***
    MAC address = [**-**-**-**-**-**]
    MAC address = [**-**-**-**-**-**]
    MAC address =[**-**-**-**-**-**]
    MAC address = [**-**-**-**-**-**]
    ***硬盘编号***
    1290******

     
     
     

    package com.sunbin.test;

    import java.io.*;

    import java.util.regex.*;

    public class ReadMacByJava {

    private String mPhysicalAddress = "";

    private int mPhysicalMacNumber = 0;

    private boolean isInit = false;

    public void init() {
    try {
    String line;
    String os = System.getProperty("os.name");
    Process process = null;
    if (os != null && os.startsWith("Windows")) {
    process = Runtime.getRuntime().exec("cmd /c ipconfig /all");
    } else {
    process = Runtime.getRuntime().exec("ifconfig eth0");
    }
    BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
    Pattern macPattern = Pattern
    .compile("([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}");
    Matcher macMatcher;
    boolean result;
    while ((line = bufferedReader.readLine()) != null) {
    if ("".equals(line)) {
    continue;
    }
    macMatcher = macPattern.matcher(line);
    result = macMatcher.find();
    if (result) {
    mPhysicalMacNumber++;
    if ("".equals(mPhysicalAddress)) {
    mPhysicalAddress = macMatcher.group(0);
    } else {
    mPhysicalAddress += ("," + macMatcher.group(0));
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    isInit = true;
    }

    public String getPhysicalAddress() {
    if (isInit) {
    return this.mPhysicalAddress;
    } else {
    return "Mac is not init.";
    }
    }


    public static void main(String[] args){
    ReadMacByJava mac = new ReadMacByJava();
    mac.init();
    System.out.println("MAC address :"+ mac.getPhysicalAddress());
    }
    }

  • 相关阅读:
    linux 下查看文件个数及大小
    weblogic日志小结
    Excel数据通过plsql导入到Oracle
    Linux查看外网IP
    linux挂载/卸载优盘
    git版本回退
    linux修改文件所属用户、用户组
    retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke(140))
    Hadoop切换namenode为active
    Netty使用LineBasedFrameDecoder解决TCP粘包/拆包
  • 原文地址:https://www.cnblogs.com/donaldlee2008/p/5452338.html
Copyright © 2011-2022 走看看