MacAddr.java
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.StringTokenizer;
public class MacAddr {
//获得机器的MAC地址
public static String getMacAddress() throws IOException {
String os = System.getProperty("os.name");//操作系统名称
String macStr = "";//MAC地址
try {
if (os.startsWith("Windows")) {
macStr = windowsParseMacAddress(windowsRunIpConfigCommand());
} else if (os.startsWith("Linux")) {
macStr = linuxParseMacAddress(linuxRunIfConfigCommand());
} else {
macStr = "00-00-00-00-00-00";
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
return macStr;
}
}
/**
* TODO:linux系统,获得ipconfig 命令得到的打印信息串
* @param ipConfigResponse String
* @return String
*/
private static String linuxParseMacAddress(String ipConfigResponse){
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
// see if line contains IP address
if (containsLocalHost && lastMacAddress != null) {
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf("HWaddr");
if (macAddressPosition <= 0)
continue;
String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
if (linuxIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
return lastMacAddress;
}
/**
* TODO:判断是否为mac地址串
* @param macAddressCandidate String
* @return boolean
*/
private static boolean linuxIsMacAddress(String macAddressCandidate) {
// TODO: use a smart regular expression
if (macAddressCandidate.length() == 17){
return true;
}else{
return false;
}
}
/**
* TODO:linux系统,获得ifconfig 命令得到的打印信息串
* @throws IOException
* @return String
*/
private static String linuxRunIfConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ifconfig");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* TODO:获得windows操作系统的MAC地址
* @param ipConfigResponse String
* @throws ParseException
* @return String
*/
private static String windowsParseMacAddress(String ipConfigResponse){
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
}
//构造一个字符串分析器,以换行符作为定界符
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null) {
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0){
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
if (windowsIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
return lastMacAddress;
}
/**
* TODO:判断是否为mac地址串
* @param macAddressCandidate String
* @return boolean
*/
private static boolean windowsIsMacAddress(String macAddressCandidate) {
//mac地址格式:00-E0-4C-30-BA-01
if (macAddressCandidate.length()==17&&macAddressCandidate.indexOf("-")>0){
return true;
}else{
return false;
}
}
/**
* TODO:windows系统,获得ipconfig /all 命令得到的打印信息串
* @throws IOException
* @return String
*/
private static String windowsRunIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ipconfig /all");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
public static void main(String[] args) {
try {
System.out.println(" Operating System Name:"+ System.getProperty("os.name"));
System.out.println(" IP/Localhost: "+ InetAddress.getLocalHost().getHostAddress());
System.out.println(" MAC Address: " + getMacAddress());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
分享到:
相关推荐
java获取本机网卡地址工具类,ipv4地址获取,根据主机名获取主网卡地址。
电脑mac物理网卡地址修改工具是一款可以管理与修改电脑网卡的mac物理地址的软件
获取网卡物理地址工具是一款获取远程网卡MAC地址的免费辅助工具(根据IP地址获取网卡MAC地址),小巧实用。
直接修改网卡物理地址,自动记忆原网卡物理地址
修改物理网卡地址 修改物理网卡地址 子网计算工具 子网计算工具 子网计算工具
通过该工具类可以获得请求的IP地址,适用于后台管理系统的信息统计。
读取网卡物理地址小工具 读取网卡物理地址小工具 exe可执行文件
IpUtil工具类--通过request获取IP,通过IP获取地址(需要联网,调用淘宝的IP库)。使用场景:在网站开发记录用户增删改查日志的时候,往往需要记录操作人的IP地址,来准确的生成操作日志记录等
修改电脑mac物理地址工具,在[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0007\Ndi\Params 下增加相应参数,在的网卡(右键属性)配置中高级tab就能见到...
NULL 博文链接:https://fanshuyao.iteye.com/blog/2436489
获取电脑mac地址工具,一键获取电脑mac,方便简单好用。
简述:通过该工具可以获取电脑相关配置信息,主要如下 1、主机名信息:主机名称、工作组/域 2、主机型号信息:品牌、型号 3、CPU信息:型号、核心数、线程 4、主板信息:制造商、型号、序列号 5、BIOS信息:厂商、...
获取本地IP地址,获取本地Host名称,获取Linux下的IP地址,获取用户真实IP地址,get/post请求 相应的工具类
工具类-获取访问ip地址工具类,自用留存
修改网卡MAC地址工具,修改网卡物理地址
利用淘宝IP地址库实现的利用IP获取用户所在地工具类
用来获取网卡MAC地址的小工具,容量很小但是绝好用,
本源码包含公众号菜单初始化开发,菜单事件开发,普通token和网页授权token开发工具类,js_ticket获取和缓存,获取openid开发,公众号网络请求封装,xml格式转换工具类,文件上传工具类,关注事件/取消关注事件开发...
电脑网卡mac地址修改工具,可以修复您出现异常的网卡MAC地址,是电脑网络通讯恢复正常