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. (一)Spring’s MVC Architecture

    Spring’s MVC module Spring’s MVC module is based on front controller design pattern followed by MVC ...

  2. MySQL用变量的方法添加伪序号列(自增序列)

    在进行数据筛选时,可能会用到给每一条数据配上一个唯一的序号,便于进行定位. 方法: 序号的设置:   @rownum :=@rownum + 1 AS rownum 获取序号的伪表[必须]:   (S ...

  3. 从socket开始讲IOS网络编程

    home list tags talk user rss Mac&iOS Socket 大纲 一.Socket简介 二.BSD Socket编程准备 1.地址 2.端口 3.网络字节序 4.半 ...

  4. react-native init安装指定版本的react-native

    C:\Users\ZHONGZHENHUA\imooc_gp\index.js index.js /** @format */ import React,{ Component } from 'rea ...

  5. hdu3999-The order of a Tree (二叉树的先序遍历)

    http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree Time Limit: 2000/1000 MS (Java/Ot ...

  6. MobileMovieTexture播放视频

    MobileMovieTexture插件支持IOS系统播放视频文件.简单,方便

  7. Java核心技术-接口、lambda表达式与内部类

    本章将主要介绍: 接口技术:主要用来描述类具有什么功能,而并不给出每个功能的具体实现.一个类可以实现一个或多个接口. lambda表达式:这是一种表示可以在将来的某个时间点执行的代码块的简洁方法. 内 ...

  8. 257. Binary Tree Paths返回所有深度优先的遍历

    [抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  9. Ubuntu 14.04 安装QQ2015

    作为重要的技术交流工具,Ubuntu下没有QQ还是挺不方便的. 参考链接(带软件下载链接)http://www.longene.org/forum/viewtopic.php?f=6&t=30 ...

  10. 对JS中函数的理解

    函数本质就是功能的集合 JS中函数是对象,因此,函数名实际上仅仅是一个指向函数对象的指针,不会与某个函数绑定,所以,JS中没有重载(重载就是通过传递不同类型的参数,使两个相同函数名的函数执行不同的功能 ...