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. jmeter出现卡死或内存溢出的解决方案

    故事背景:在初次使用jmeter的时候,把线程设置较大值的时候,jmeter工具很容易就卡死了,导致每次做压测的时候都无法顺利完成,非常的闹心,通过各种方法寻找解决方案,终于找到了一个比较靠谱的方法, ...

  2. 列表(list)之三 -如何较为均匀的将任意字符串按指定组数分组,方差最少

    当字符串的长度不是份数的整数倍时如何均匀地分割,例如:长度为11的字符串要分割成4份,有很多种分法,比如3, 3, 3, 2(前3个字符一份,中间3个一份,再中间3个一份,最后2个一份)这种是比较均匀 ...

  3. Opencv在mac系统的安装与试用

    1.在mac终端内,使用brew安装opencv3,这时我的opencv被安装到/usr/local/Cellar/opencv3/3.2.0内. 2.新建xcode 项目,选择command lin ...

  4. JavaScript数据结构与算法(七) 双向链表的实现

    TypeScript方式实现源码 // 双向链表和普通链表的区别在于, 在链表中, // 一个节点只有链向下一个节点的链接,而在双向链表中,链接是双向的:一个链向下一个元素, // 另一个链向前一个元 ...

  5. Idea导入多个maven项目到同一目录下

    目标 简单导入多个maven项目进入同一个project(相当于eclipse的workspace) 过程 1.新建一个目录作为仿eclipse的workspace,这里起名为idea-workspa ...

  6. ML笔记:Classification: Logistic Regression

  7. [HEOI2014]大工程

    题目描述 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...

  8. 习题10-1 UVA 11040(无聊水一水)

    题意: 给你一个残缺的塔,每个数字由他下面左右两个数相加得.给你其中一部分,要求输出全部的数字. #include <iostream> #include <cstdio> # ...

  9. Codeforces Round #398 (div.2)简要题解

    这场cf时间特别好,周六下午,于是就打了打(谁叫我永远1800上不去div1) 比以前div2的题目更均衡了,没有太简单和太难的...好像B题难度高了很多,然后卡了很多人. 然后我最后做了四题,E题感 ...

  10. 触发事件trigger

    触发事件,在本例中,文档加载好之后,就触发dblclick双击事件,而不是通过去手动双击. <script src="http://how2j.cn/study/jquery.min. ...