java 通过ip获取客户端mac地址
java 通过ip获取客户端mac地址
package com.asppro.util; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class MacUtil { public static String getLocalMacByIp(String ip) throws SocketException, IOException{
NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(ip));
byte[]mac=ne.getHardwareAddress();
String mac_s=hexByte(mac[0])+"-"+
hexByte(mac[1])+"-"+
hexByte(mac[2])+"-"+
hexByte(mac[3])+"-"+
hexByte(mac[4])+"-"+
hexByte(mac[5])
;
return mac_s;
} private static String hexByte(byte b)
{
String s="000000"+Integer.toHexString(b);
return s.substring(s.length()-2);
} /**
* 获取当前操作系统名称. return 操作系统名称 例如:windows,Linux,Unix等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
} /**
* 获取Unix网卡的mac地址.
*
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
/**
* Unix下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
*/
process = Runtime.getRuntime().exec("ifconfig eth0");
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
/**
* 寻找标示字符串[hwaddr]
*/
index = line.toLowerCase().indexOf("hwaddr");
/**
* 找到了
*/
if (index != -1) {
/**
* 取出mac地址并去除2边空格
*/
mac = line.substring(index + "hwaddr".length() + 1).trim();
break;
}
}
} catch (IOException e) {
e.getMessage();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
} /**
* 获取Linux网卡的mac地址.
*
* @return mac地址
*/
public static String getLinuxMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
/**
* linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
*/
process = Runtime.getRuntime().exec("ifconfig eth0");
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("硬件地址");
/**
* 找到了
*/
if (index != -1) {
/**
* 取出mac地址并去除2边空格
*/
mac = line.substring(index + 4).trim();
break;
}
}
} catch (IOException e) {
e.getMessage();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
} return mac;
} /**
* 获取widnows网卡的mac地址.
*
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
/**
* windows下的命令,显示信息中包含有mac地址信息
*/
process = Runtime.getRuntime().exec("ipconfig /all");
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream(), "GBK")); // windows系统都是GBK编码,不加GBK读出的中文是乱码
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
/**
* 寻找标示字符串[physical address]
*/
index = line.toLowerCase().indexOf("physical address");
if (index == -1) {
/**
* 寻找标示字符串[物理地址]
*/
index = line.toLowerCase().indexOf("物理地址"); } if (index != -1) {
index = line.indexOf(":");
if (index != -1) {
/**
* 取出mac地址并去除2边空格
*/
mac = line.substring(index + 1).trim();
}
break;
}
}
} catch (IOException e) {
e.getMessage();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
} /**
* 获取widnows网卡的mac地址.
*
* @return mac地址
*/
public static String getWindowsMACAddress(String ip) {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
/**
* windows下的命令,显示信息中包含有mac地址信息
*/
// process = Runtime.getRuntime().exec("ipconfig /all");
process = Runtime.getRuntime().exec("nbtstat -A " + ip);
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream(), "GBK")); // windows系统都是GBK编码,不加GBK读出的中文是乱码
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
/**
* 寻找标示字符串[physical address]
*/
index = line.toLowerCase().indexOf("mac 地址 ="); if (index != -1) {
index = line.indexOf("=");
if (index != -1) {
/**
* 取出mac地址并去除2边空格
*/
mac = line.substring(index + 1).trim();
}
break;
}
}
} catch (IOException e) {
e.getMessage();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
} /**
* 获取客户端mac
*
* @description {TODO}
* @return
*/
public static String getMACAddress(String ip) {
String os = getOSName();
String mac = "";
if (os.startsWith("windows")) {
mac = getWindowsMACAddress(ip);
} else if (os.startsWith("linux")) {
mac = getLinuxMACAddress();
} else {
mac = getUnixMACAddress();
}
return mac;
} }
java 通过ip获取客户端mac地址的更多相关文章
- js获取客户端MAC地址
最近遇到一个需求,医院要求呼叫中心账号必须对应MAC地址,也就是说该MAC地址必须和呼叫中心账号对应才可使用,这可就难道我了,这需求就要求每次都判断用户登录的电脑MAC地址是否有呼叫中心账号,当然只针 ...
- js 获取客户端mac地址
js 获取客户端mac地址 javascript获取客户端网卡MAC地址和IP地址和计算机名 nodesj如何获得客户端的mac地址呢? 浏览器获取MAC地址 不限浏览器的mac地址取得的几种办法 I ...
- 获取客户端Mac地址
近期有个需求,需要获取客户端Mac地址作为白名单验证的依据.使用.net,B/S架构.先百度找了一些获取mac地址的方法, using System; using System.Collections ...
- 如何获取客户端MAC地址(三个方法)
方法一: 调用Windows的DOS命令,从输出结果中读取MAC地址: public static String getMACAddress() { String address = "&q ...
- php/js获取客户端mac地址的实现代码
这篇文章主要介绍了如何在php与js中分别获取客户度mac地址的方法,需要的朋友可以参考下 废话不多讲,直接上代码吧! 复制代码 代码如下: <?php class MacAddr { ...
- http协议本身能获取客户端Mac地址问题
http 位于网络应用程 应用层 会话层 表示层 传输层 网络层 数据链路层 物理层 数据在最高层开始传输 没经历下面一层加一层的头,然后传入目的电脑再进行一层层的解刨,所以http本来没有mac而接 ...
- 关于获取客户端Mac地址
private static string GetClientMAC() { string mac_dest = string.Empty; try { string strClientIP = Ht ...
- web网站获取客户端mac地址
<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE> <META http-equiv=Content-Ty ...
- php获取客户端mac地址
exec('/sbin/arp -a 2>&1', $array, $return_val);dump($array);$mac = '';foreach($array as $valu ...
随机推荐
- [ 原创 ] Linux下查找指定类型文件以及删除
find ./ -name "*.lok" // 查找文件find ./ -name "*.lok" |xargs rm -fr // 查找文件并删除
- EXPLAIN sql优化方法(2) Using temporary ; Using filesort
优化GROUP BY语句 默认情况下,MySQL对所有GROUP BY col1,col2...的字段进行排序.这与在查询中指定ORDER BY col1,col2...类似.因此,如果显式包括一 ...
- 添加 MyEclipse Persistence Tools 类库
1).右键点击你的项目,然后选择Properties.2).在 Java Build Path 页面, 在 Libraries 面板下选择 Add Library….3).选择 MyEclipse L ...
- [转]字符集、字符编码、XML中的中文编码
字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...
- RimLight(轮廓光) - Shader
[RimLight(轮廓光) - Shader] RimLight指的是物体的轮廓光.效果如下: 轮廓光的强度通过 1.0 - dot(normal, eye_vector)来计算.使用这个公式,则指 ...
- 使用Windows绘图合成多张图
[使用Windows绘图合成多张图] 1.点击图片右键选择打开方式→选择画图工具打开图片. 2.这时图片可以改变外框大小,将外框尽量弄大些. 3.点击编辑选中下拉菜单中的粘贴来源,选择需要增加进来的图 ...
- 第五章 大数据平台与技术 第12讲 大数据处理平台Spark
Spark支持多种的编程语言 对比scala和Java编程上节课的计数程序.相比之下,scala简洁明了. Hadoop的IO开销大导致了延迟高,也就是说任务和任务之间涉及到I/O操作.前一个任务完成 ...
- c# 遍历一个对象里面的全部属性
比如我现在有一个Student的对象,里面有属性stuName,stuAge,stuGender,我现在该怎么写循环才能遍历这几个属性? Student s=new...... foreach (Sy ...
- 341. Flatten Nested List Iterator展开多层数组
[抄题]: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...
- Java 面试知识点汇总
OOP:(Object Oriented Programming )面向对象编程 重用性.灵活性和扩展性 高内聚.低耦合 面向过程编程与面向对象编程的区别:举例,自己做饭吃与去饭馆吃,去饭馆只需要知道 ...