import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader; public class TestMac {
public static void main(String[] args) {
System.out.println("Operation System=" + getOsName());
System.out.println("Mac Address=" + getMACAddress());
System.out.println("通过ip获取mac" + getMACAddress("*.*.*.*"));
} public static String getOsName() {
String os = "";
os = System.getProperty("os.name"); return os;
} public static String getMACAddress() {
String address = "";
String os = getOsName(); if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig/all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line; while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index); break;
}
} br.close(); return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p; try {
p = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line; while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index); break;
}
} br.close();
} catch (IOException e) {
}
} address = address.trim(); return address;
} public static String getMACAddress(String ipAddress) {
String str = "";
String strMAC = "";
String macAddress = ""; try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) {
str = input.readLine(); if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length()); break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
} //
if (strMAC.length() < 17) {
return "Error!";
} macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) +
":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) + ":" +
strMAC.substring(12, 14) + ":" + strMAC.substring(15, 17); //
return macAddress;
}
}

Windows下Java如何调用本地获取mac地址的更多相关文章

  1. Windows下Java调用BAT批处理不弹出cmd窗口

    常规Windows下Java调用BAT方式肯定会弹出cmd窗口 Runtime.getRuntime().exec("cmd.exe /C start D:\\test.bat") ...

  2. java获取本地计算机MAC地址

    java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...

  3. Java根据ip地址获取Mac地址,Java获取Mac地址

    Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...

  4. 本地windows下搭建git的本地服务器

    本地windows下搭建git的本地服务器 准备工作: 本地安装java环境,配置环境变量(略) 下载gitblit文件,百度一大堆 开始第一步: 减压gitblit压缩包到某个目录下,比如我在:H: ...

  5. Java -- 获取MAC地址

    啦啦啦 package com.xindatai.common.util; import java.io.InputStream; import java.util.regex.Matcher; im ...

  6. Windows 下java环境变量的配置(Windows7 ,8,8.1,10)

    Windows 下java环境变量的配置 在“系统”面板的左上角选择“高级系统设置”,在弹出的系统属性中选择”高级“项,然后点击右下角的“环境变量(N)...”,就此进入JAVA环境变量的配置. 如果 ...

  7. c++ windows 获取mac地址

    c++ windows 获取mac地址 GetAdaptersInfo 用windows api获取mac地址和硬盘id编号 aa

  8. android获取Mac地址和IP地址

    获取Mac地址实际项目中测试了如下几种方法:(1)设备开通Wifi连接,获取到网卡的MAC地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法) //根据Wifi信 ...

  9. 在Web中获取MAC地址

    很多时候都很难琢磨客户在想什么,也许是自己业务经验不足,也许是客户要显示出他在软件方面也非常的专业.记得以前听过一个故事,说一个富人想娶个媳妇,然后他比较钟意的有三个女人,然后就想从三个女人中选一个, ...

随机推荐

  1. SQL基础-----DML语句

    之前已经介绍过SQL基础之DDL(数据库定义语言)语句,http://www.cnblogs.com/cxq0017/p/6433938.html(这是地址) 这篇文章主要介绍DML语句(数据库操纵语 ...

  2. 常见web安全隐患及解决方案

    Abstract 有关于WEB服务以及web应用的一些安全隐患总结资料. 1. 常见web安全隐患 1.1.       完全信赖用户提交内容 开发人员决不能相信一个来自外部的数据.不管它来自用户提交 ...

  3. [LeetCode] Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  4. [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  5. python系列之 - 并发编程(进程池,线程池,协程)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  6. [SDOI2011]计算器

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...

  7. ●BZOJ 1444 [Jsoi2009]有趣的游戏

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1444题解.1: 概率dp,矩阵乘法,快速幂. 对所有串建立AC自动机, 那么如果在trie树 ...

  8. ●SPOJ 1811 Longest Common Substring

    题链: http://poj.org/problem?id=2774 题解: 求两个字符串(S,T)的最长公共子串.对 S串建后缀自动机.接下来就用这个自动机去求出能和 S串匹配的 T的每一个前缀的最 ...

  9. hdu2795 线段树 贴广告

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. 在右键中添加以管理员运行CMD命令提示符 (进化版)

    直接代码,转过来的 20180316更新添加快捷键A,点右键按A即可: Windows Registry Editor Version 5.00 ; Created by: Shawn Brink ; ...