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地址的更多相关文章

  1. js获取客户端MAC地址

    最近遇到一个需求,医院要求呼叫中心账号必须对应MAC地址,也就是说该MAC地址必须和呼叫中心账号对应才可使用,这可就难道我了,这需求就要求每次都判断用户登录的电脑MAC地址是否有呼叫中心账号,当然只针 ...

  2. js 获取客户端mac地址

    js 获取客户端mac地址 javascript获取客户端网卡MAC地址和IP地址和计算机名 nodesj如何获得客户端的mac地址呢? 浏览器获取MAC地址 不限浏览器的mac地址取得的几种办法 I ...

  3. 获取客户端Mac地址

    近期有个需求,需要获取客户端Mac地址作为白名单验证的依据.使用.net,B/S架构.先百度找了一些获取mac地址的方法, using System; using System.Collections ...

  4. 如何获取客户端MAC地址(三个方法)

    方法一: 调用Windows的DOS命令,从输出结果中读取MAC地址: public static String getMACAddress() { String address = "&q ...

  5. php/js获取客户端mac地址的实现代码

    这篇文章主要介绍了如何在php与js中分别获取客户度mac地址的方法,需要的朋友可以参考下   废话不多讲,直接上代码吧! 复制代码 代码如下: <?php   class MacAddr {  ...

  6. http协议本身能获取客户端Mac地址问题

    http 位于网络应用程 应用层 会话层 表示层 传输层 网络层 数据链路层 物理层 数据在最高层开始传输 没经历下面一层加一层的头,然后传入目的电脑再进行一层层的解刨,所以http本来没有mac而接 ...

  7. 关于获取客户端Mac地址

    private static string GetClientMAC() { string mac_dest = string.Empty; try { string strClientIP = Ht ...

  8. web网站获取客户端mac地址

    <HTML><HEAD><TITLE>WMI Scripting HTML</TITLE> <META http-equiv=Content-Ty ...

  9. php获取客户端mac地址

    exec('/sbin/arp -a 2>&1', $array, $return_val);dump($array);$mac = '';foreach($array as $valu ...

随机推荐

  1. Linux运维跳槽必备的40道面试精华题

    过一次年,结婚.存款.父母养老,一系列向钱看的事都在碾压我们本来还挺简单的神经,但难过没有出路,唯有找到好的方法和事业方向,才能实现一步一个脚印的逆袭. 下面是一名资深Linux运维求职数十家公司总结 ...

  2. C语言增量内存申请 realloc

    void* realloc (void* ptr, size_t size); Reallocate memory block Changes the size of the memory block ...

  3. LightGBM

    1.简介 lightGBM包含两个关键点:light即轻量级,GBM 梯度提升机 LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势 ...

  4. overflow与underflow

    是新近的firefox浏览器中支持overflow, underflow这两个事件,当某一元素的大小超出父元素的显示范围就会触发overflow事件,如果从超出显示再变回不超出的状态则触发underf ...

  5. 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制

    权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了. 用反射和自定义注解来实 ...

  6. spring 提供的属性值拷贝工具类

    当需要把一个原生的类中属性拷贝到扩展类中时,使用以下类很方便:

  7. 选择性导出excel表中内容

    package com.huawei.utils; import java.io.FileNotFoundException;import java.io.FileOutputStream;impor ...

  8. 在Yii Framework中利用PHPMailer发送邮件(2011-06-02 14:06:23)

    转载▼ 标签: it 分类: 技术共享 官方扩展链接:http://www.yiiframework.com/extension/mailer/这个扩展配置十分方便,如果有问题的话,可以打开Debug ...

  9. Culling & Depth Testing

    [Culling & Depth Testing] Culling is an optimization that does not render polygons facing away f ...

  10. Jquery循环select标签,并给指定option添加select属性后在页面上不显示的问题

    <select id="testId"> <option value="">--请选择--</option> <opt ...