java获取硬盘ID以及MAC地址
http://blog.csdn.net/coolwzjcool/article/details/6698327
版权声明:本文为博主原创文章,未经博主允许不得转载。
为了达到软件注册,或者说软件和电脑绑定的目的,需要将电脑上的固定编号进行一系列的算法计算,并生成唯一和软件匹配的号码。
那么使用java如何达到这个目的呢?
通常做法都是通过java的Runtime来完成,通过 process的输入流,进行获取相关的信息。
下面列举具体的例子:
1、DiskUtils 获取硬盘编号
- import java.io.File;
- import java.io.FileWriter;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- class DiskUtils {
- private DiskUtils() {
- }
- public static String getSerialNumber(String drive) {
- String result = "";
- try {
- File file = File.createTempFile("damn", ".vbs");
- file.deleteOnExit();
- FileWriter fw = new java.io.FileWriter(file);
- String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
- + "Set colDrives = objFSO.Drives\n"
- + "Set objDrive = colDrives.item(\""
- + drive
- + "\")\n"
- + "Wscript.Echo objDrive.SerialNumber"; // see note
- fw.write(vbs);
- fw.close();
- Process p = Runtime.getRuntime().exec(
- "cscript //NoLogo " + file.getPath());
- BufferedReader input = new BufferedReader(new InputStreamReader(
- p.getInputStream()));
- String line;
- while ((line = input.readLine()) != null) {
- result += line;
- }
- input.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result.trim();
- }
- }
2、MacUtils 获取MAC地址
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- public class MacUtils {
- public static void getMac(){
- try {
- Process process = Runtime.getRuntime().exec("ipconfig /all");
- InputStreamReader ir = new InputStreamReader(process.getInputStream());
- LineNumberReader input = new LineNumberReader(ir);
- String line;
- while ((line = input.readLine()) != null)
- if (line.indexOf("Physical Address") > 0) {
- String MACAddr = line.substring(line.indexOf("-") - 2);
- System.out.println("MAC address = [" + MACAddr + "]");
- }
- } catch (java.io.IOException e) {
- System.err.println("IOException " + e.getMessage());
- }
- }
- }
3、 测试程序:
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.util.Enumeration;
- import java.util.Vector;
- public class TestMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //****************获取MAC地址*****************//
- System.out.println("***MAC地址***");
- MacUtils.getMac();
- //****************获取硬盘ID*****************//
- String sn = DiskUtils.getSerialNumber("C");
- System.out.println("***硬盘编号***");
- System.out.println(sn);
- }
- }
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());
}
}
java获取硬盘ID以及MAC地址的更多相关文章
- Java获取主板序列号、MAC地址、CPU序列号工具类
import java.io.File; import java.io.FileWriter; import java.io.BufferedReader; import java.io.IOExce ...
- JAVA获取本机的MAC地址
/** * 获取本机的Mac地址 * @return */ public String getMac() { InetAddress ia; byte[] mac = null; try { // 获 ...
- java支持跨平台获取cpuid、主板id、硬盘id、mac地址 (兼容windows、Linux)
windows: package cn.net.comsys.helper.system.info; import java.io.BufferedReader; import java.io.F ...
- java 获取计算机名称, ip, mac地址
写在前面 通常所说查询本机mac地址是以以太网为准的, 也就是网线那个口..这种描述略捞. 但是通过java的getHostAddress获取ip以及getHardwareAddress()方法获取m ...
- C#获取cpu序列号 硬盘ID 网卡硬地址以及操作注册表 .
转:http://blog.csdn.net/smartsmile2012/article/details/8682295 #region 获取cpu序列号 硬盘ID 网卡硬地址 /**/ /// & ...
- java获取客户端ID地址
转:http://zhenchengchagangzi.iteye.com/blog/1199300#bc2372048 在JSP里,获取客户端的IP地址的方法是:request.getRemoteA ...
- C# 中获取CPU序列号/网卡mac地址
1.cpu序列号2.mac序列号3.硬盘id在给软件加序列号时这三个应该是最有用的,可以实现序列号和机器绑定,对保护软件很有好处.哈哈. using System; using System.Ma ...
- ASP.NET获取客户端IP及MAC地址
朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了:下边分为了C#后台获取的方法和前台Javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_ ...
- 如何获取公网IP的mac地址
如何获取远程IP的mac地址 思路分析 由于java本身没有相关的jar包进行获取,所以这里介绍从其他的方面进行入手和实践 使用的工具对比: tcpdump tshark pcap4j 都可以达到抓包 ...
随机推荐
- BFS and Queue
BFS (Bridth First Search) can be implemented by a queue. Procedure is like this: (Q is Queue) 1, Put ...
- ios小功能
1.开 发过程中,我们通过http请求,后台返回json数据,而有时数据里某一字段的值为null-,然后我们把此值赋值给 NSArray,NSdictionary,或是NSString,然后我们会判断 ...
- 个人linux简单笔记,随时更新
vim显示行数 :set nu 查找文件 find /home -name config.txt 重命名文件或者文件夹 mv a b centos中phpize的安装 yum install php- ...
- 收缩sql server数据库日志
项目中,可能数据库(sql server数据库)日志太多,占了很多磁盘空间,可以通过收缩数据库日志,减少日志文件大小. 下面以Northwind数据库为例: 1.把数据库的恢复模式设置为“简单模式”: ...
- jsp环境搭建(Windows)
1.软件准备 JDK 最新版jdk-8u45-windows-x64.exe tomcat 最新版32-bit/64-bit Windows Service Installer Eclipse IDE ...
- linux的学习系列 9--网络通信
ping 命令 ping 命令会向网络上的主机发送应答请求,根据响应信息可以判断远程主机是否可用. ping 命令的语法: $ping hostname or ip-address 如果网络畅通,很快 ...
- Keychain 浅析
什么是Keychain? 根据苹果的介绍,iOS设备中的Keychain是一个安全的存储容器,可以用来为不同应用保存敏感信息比如用户名,密码,网络密码,认证令牌.苹果自己用keychain来保存Wi- ...
- building system busy, pls wait !!
编译ca是可能会报这个错误,是189服务器上的/home/pub-work/.android_build_lock这个文件的问题,删除即可.
- linker command failed with exit code 1 (use -v to see invocation)修改方法
它的意思是指文件丢失或者文件重复,你就要仔细查看项目中有没有重复的文件,如有重复修改即可,如果是文件丢失,那就麻烦啦!你需要根据报错的地方进行修补文件,最为直接的方法是重新创建项目.还有另外一种情况是 ...
- linux下的vim使用笔记
环境:window下可以使用gvim编辑软件 学习主要是在ubuntu15敲击命令学习的视频来自于智普教育vim使用视频1. sudo apt show vi 查看安装的vi版本,当然了我的ubunt ...