java获取IP地址和MAC地址方式
前端和后台都可以获取
ip获取方式比较简单,就不做分析了,主要说mac的获取
前端获取的方式与局限性:尝试好几种方式,这种是最易实现、最简单的方式,确实能获取该浏览器客户端的mac地址
局限性:首先并不是所有浏览器都支持,笔者主要对ie做了测试,还要设置ie的安全设置(internet选项–》安全–》自定义设置 设置启用active X,如图)
方式上:
/*$(function () {
getMacAddress();
})
//js获取mac地址,需要在ie上安全设置上启用activeX,所以选用后台去获取mac地址方案
function getMacAddress() {
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled =True");
var e = new Enumerator(properties); {
var p = e.item();
var mac = p.MACAddress;
document.getElementById("mac").value = mac;
}
}*/
Java后台获取方式上:
1.获取ip地址
/**
* @description:根据请求获取ip地址
* @author: Lmc
* @time: 2020/5/6 0006 10:25
*/
import javax.servlet.http.HttpServletRequest;
public class IpUtil {
/**
* 获取登录用户IP地址
*
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
2.获取mac地址:
方式一(不推荐):使用本地与其他主机分开的方式,特点上是通过ip地址请求获取mac地址,使用命令行 "nbtstat -A " + ipAddress 的方式,解析返回的流获取mac地址
优缺点:简单粗暴,很容易理解,能对很多机器识别
首先速度慢,很慢,笔者测试的是8秒左右,若某机器修改过ip地址,并不能获取到mac地址
public static String getLocalMac(String ipAddress) throws SocketException,
UnknownHostException {
// TODO Auto-generated method stub
String str = "";
String macAddress = "";
final String LOOPBACK_ADDRESS = "127.0.0.1";
// 如果为127.0.0.1,则获取本地MAC地址。
if (LOOPBACK_ADDRESS.equals(ipAddress)) {
InetAddress inetAddress = InetAddress.getLocalHost();
// 貌似此方法需要JDK1.6。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
.getHardwareAddress();
// 下面代码是把mac地址拼装成String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
macAddress = sb.toString().trim().toUpperCase();
return macAddress;
} else {
// 获取非本地IP的MAC地址
try {
System.out.println(ipAddress);
Process p = Runtime.getRuntime()
.exec("nbtstat -A " + ipAddress);
InputStreamReader ir = new InputStreamReader(p.getInputStream(),"gbk");
BufferedReader br = new BufferedReader(ir);
String s="";
while ((str = br.readLine()) != null) {
s+=str;
}
while ((str = br.readLine()) != null) {
if (str.indexOf("MAC") > 1) {
macAddress = str.substring(str.indexOf("MAC") + 9,
str.length());
macAddress = macAddress.trim();
System.out.println("macAddress:" + macAddress);
break;
}
}
p.destroy();
br.close();
ir.close();
} catch (IOException ex) {
}
return macAddress;
}
}
方式二(不推荐):也是使用命令行方式,这种方式采用liunx与widows分开的方式,正则去匹配获取第一个mac地址
优缺点:时间上比方式一要快,测试发现若某机器修改过ip地址,并不能有效获取到mac地址,甚至出现了获取实际ip地址一半的情况(实测)
String getMacInfo(HttpServletRequest request)
{
//获取ip地址
String macInfo = null;
try
{
String ip = request.getRemoteAddr();
//linux下获取mac地址
macAddr = CommonUtils.getMac(ip);
//windows下获取mac地址
if(StringUtils.isBlank(macAddr)){
macAddr = CommonUtils.getMacInWindows(ip).trim();
}
}
catch (Exception e)
{
log.error("获取mac地址失败");
return null;
}
return macInfo;
}
// 从类unix机器上获取mac地址
public static String getMac(String ip) throws IOException {
String mac = SysCode.BDFH.EMPTY;
if (ip != null) {
try {
Process process = Runtime.getRuntime().exec("arp "+ip);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
StringBuffer s = new StringBuffer();
while ((line = input.readLine()) != null) {
s.append(line);
}
mac = s.toString();
if (StringUtils.isNotBlank(mac)) {
mac = mac.substring(mac.indexOf(":") - 2, mac.lastIndexOf(":") + 3);
}
return mac;
} catch (Exception e) {
e.printStackTrace();
}
}
return mac;
}
// 从windows机器上获取mac地址
public static String getMacInWindows(final String ip) {
String result = "";
String[] cmd = {"cmd", "/c", "ping " + ip};
String[] another = {"cmd", "/c", "ipconfig -all"};
// 获取执行命令后的result
String cmdResult = callCmd(cmd, another);
// 从上一步的结果中获取mac地址
result = filterMacAddress(ip, cmdResult, "-");
return result;
}
// 命令执行
public static String callCmd(String[] cmd, String[] another) {
String result = "";
String line = "";
try {
Runtime rt = Runtime.getRuntime();
// 执行第一个命令
Process proc = rt.exec(cmd);
proc.waitFor();
// 执行第二个命令
proc = rt.exec(another);
InputStreamReader is = new InputStreamReader(proc.getInputStream());
BufferedReader br = new BufferedReader(is);
while ((line = br.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 获取mac地址
public static String filterMacAddress(final String ip, final String sourceString, final String macSeparator) {
String result = "";
String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(sourceString);
while (matcher.find()) {
result = matcher.group(1);
// 因计算机多网卡问题,截取紧靠IP后的第一个mac地址
int num = sourceString.indexOf(ip) - sourceString.indexOf(": "+result + " ");
if (num>0&&num<300) {
break;
}
}
return result;
}
方式三(推荐)、采用网络编程(DatagramSocket && DatagramPacket)
优缺点:识别快、准确度高
import java.io.IOException;
import java.net.*;
/**根据ip获取mac地址
* @author lmc
*/
public class MacUtil {
private String sRemoteAddr;
private int iRemotePort = 137;
private byte[] buffer = new byte[1024];
private DatagramSocket ds = null;
public MacUtil(String strAddr) throws Exception {
sRemoteAddr = strAddr;
ds = new DatagramSocket();
}
protected final DatagramPacket send(final byte[] bytes) throws IOException {
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress
.getByName(sRemoteAddr), iRemotePort);
ds.send(dp);
return dp;
}
protected final DatagramPacket receive() throws Exception {
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
return dp;
}
// 询问包结构:
// Transaction ID 两字节(16位) 0x00 0x00
// Flags 两字节(16位) 0x00 0x10
// Questions 两字节(16位) 0x00 0x01
// AnswerRRs 两字节(16位) 0x00 0x00
// AuthorityRRs 两字节(16位) 0x00 0x00
// AdditionalRRs 两字节(16位) 0x00 0x00
// Name:array [1..34] 0x20 0x43 0x4B 0x41(30个) 0x00 ;
// Type:NBSTAT 两字节 0x00 0x21
// Class:INET 两字节(16位)0x00 0x01
protected byte[] getQueryCmd() throws Exception {
byte[] t_ns = new byte[50];
t_ns[0] = 0x00;
t_ns[1] = 0x00;
t_ns[2] = 0x00;
t_ns[3] = 0x10;
t_ns[4] = 0x00;
t_ns[5] = 0x01;
t_ns[6] = 0x00;
t_ns[7] = 0x00;
t_ns[8] = 0x00;
t_ns[9] = 0x00;
t_ns[10] = 0x00;
t_ns[11] = 0x00;
t_ns[12] = 0x20;
t_ns[13] = 0x43;
t_ns[14] = 0x4B;
for (int i = 15; i < 45; i++) {
t_ns[i] = 0x41;
}
t_ns[45] = 0x00;
t_ns[46] = 0x00;
t_ns[47] = 0x21;
t_ns[48] = 0x00;
t_ns[49] = 0x01;
return t_ns;
}
// 表1 “UDP-NetBIOS-NS”应答包的结构及主要字段一览表
// 序号 字段名 长度
// 1 Transaction ID 两字节(16位)
// 2 Flags 两字节(16位)
// 3 Questions 两字节(16位)
// 4 AnswerRRs 两字节(16位)
// 5 AuthorityRRs 两字节(16位)
// 6 AdditionalRRs 两字节(16位)
// 7 Name<Workstation/Redirector> 34字节(272位)
// 8 Type:NBSTAT 两字节(16位)
// 9 Class:INET 两字节(16位)
// 10 Time To Live 四字节(32位)
// 11 Length 两字节(16位)
// 12 Number of name 一个字节(8位)
// NetBIOS Name Info 18×Number Of Name字节
// Unit ID 6字节(48位
protected final String getMacAddr(byte[] brevdata) throws Exception {
// 获取计算机名
int i = brevdata[56] * 18 + 56;
String sAddr = "";
StringBuffer sb = new StringBuffer(17);
// 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每个NetBIOS Names
// Info部分占18个字节)
// 然后可计算出“Unit ID”字段的位置=56+Number Of
// Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。
for (int j = 1; j < 7; j++) {
sAddr = Integer.toHexString(0xFF & brevdata[i + j]);
if (sAddr.length() < 2) {
sb.append(0);
}
sb.append(sAddr.toUpperCase());
if (j < 6)
sb.append('-');
}
return sb.toString();
}
public final void close() {
try {
ds.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public final String getRemoteMacAddr() throws Exception {
byte[] bqcmd = getQueryCmd();
send(bqcmd);
DatagramPacket dp = receive();
String smac = getMacAddr(dp.getData());
close();
return smac;
}
/**
* 根据ip地址获取mac,这里其实还可以再次封装作为工具类
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
MacUtil macUtil = new MacUtil("172.16.22.22");
String macAddress = macUtil.getRemoteMacAddr();
System.out.println(macAddress);
}
}
声明:以上方式均来自于网络,根据自己的实际需求做的测试与使用,没有贬低任何一种实现方式的意思,如有侵权请联系删除