Windows下Java如何调用本地获取mac地址
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地址的更多相关文章
- Windows下Java调用BAT批处理不弹出cmd窗口
常规Windows下Java调用BAT方式肯定会弹出cmd窗口 Runtime.getRuntime().exec("cmd.exe /C start D:\\test.bat") ...
- java获取本地计算机MAC地址
java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...
- Java根据ip地址获取Mac地址,Java获取Mac地址
Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...
- 本地windows下搭建git的本地服务器
本地windows下搭建git的本地服务器 准备工作: 本地安装java环境,配置环境变量(略) 下载gitblit文件,百度一大堆 开始第一步: 减压gitblit压缩包到某个目录下,比如我在:H: ...
- Java -- 获取MAC地址
啦啦啦 package com.xindatai.common.util; import java.io.InputStream; import java.util.regex.Matcher; im ...
- Windows 下java环境变量的配置(Windows7 ,8,8.1,10)
Windows 下java环境变量的配置 在“系统”面板的左上角选择“高级系统设置”,在弹出的系统属性中选择”高级“项,然后点击右下角的“环境变量(N)...”,就此进入JAVA环境变量的配置. 如果 ...
- c++ windows 获取mac地址
c++ windows 获取mac地址 GetAdaptersInfo 用windows api获取mac地址和硬盘id编号 aa
- android获取Mac地址和IP地址
获取Mac地址实际项目中测试了如下几种方法:(1)设备开通Wifi连接,获取到网卡的MAC地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法) //根据Wifi信 ...
- 在Web中获取MAC地址
很多时候都很难琢磨客户在想什么,也许是自己业务经验不足,也许是客户要显示出他在软件方面也非常的专业.记得以前听过一个故事,说一个富人想娶个媳妇,然后他比较钟意的有三个女人,然后就想从三个女人中选一个, ...
随机推荐
- Java基础学习(五)-- Java中常用的工具类、枚举、Java中的单例模式之详解
Java中的常用类 1.Math : 位于java.lang包中 (1)Math.PI:返回一个最接近圆周率的 (2)Math.abs(-10):返回一个数的绝对值 (3)Math.cbrt(27): ...
- 阿里云、腾讯云开通端口 telnet不通的原因
1.安全组是否已经开通相对应的端口: 阿里云:https://help.aliyun.com/document_detail/25471.html 腾讯云:http://bbs.qcloud.com/ ...
- Goldwell平台官网简介-欢迎咨询经理罗琪
Goldwell平台官网简介-欢迎咨询经理罗琪1: Goldwell官网是一家国际衍生品的经纪公司.它对柬埔寨金融市场和客户绝对的承诺,在SECC的监管和保护下,提供了更加多元化的金融交易工具. Go ...
- [LeetCode] Largest Plus Sign 最大的加型符号
In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- TCP三次握手详解及释放连接过程
TCP在传输之前会进行三次沟通,一般称为"三次握手",传完数据断开的时候要进行四次沟通,一般称为"四次挥手". 两个序号和三个标志位: (1)序号:seq序号, ...
- Java数据类型与SQL数据类型的映射
Java数据类型与SQL数据类型的映射 SQL Data Type Java Data Type char/varchar/longvarchar String numeric/decimal jav ...
- Android ART、Dalvik在multidex上的差异、关联
为提升应用运行性能,谷歌官方从5.0(api level:21)版本开始,将虚拟机运行环境默认为ART, 此处主要研究ART.Dalvik在multidex处理上的差异和关联,做了一个简单的手绘,如下 ...
- 十大面试难题解惑,看完秒杀一切 HR 面。程序员必读!
最能体现求职者能力的就是面试,能不能拿到Offer,取决于你面试时的表现,只有有准备才能在面试过程中游刃有余. 小编收集了10个面试官最爱提的问题,虽然题目千变万化,但是万变不离其宗,只要掌握了答题的 ...
- [HNOI2015]接水果
题目描述 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, 她觉得这个游戏太简单了,于是发明了一个更加难的版本. 首先有 ...