首页

关于定义NetworkUtils网络工具类实现获取外网或内网的MAC网卡物理及IP地址的代码示例说明

标签:NetworkUtils,网络工具类,mac网卡,外内网网络信息     发布时间:2019-01-25   

一、前言

通过定义NetworkUtils网络工具类,实现外网或内网环境下获取客户端mac网卡地址信息(可以参考 java.net.NetworkInterface方式实现 ),详情参见代码示例。

二、代码示例

1. UdpGetClientMacAddr解析类 - 从DatagramPacket网络包中解析获取mac地址信息

package com.xwood.fs.util;@b@@b@import java.io.IOException;@b@import java.net.DatagramPacket;@b@import java.net.DatagramSocket;@b@import java.net.InetAddress;@b@  @b@/** @b@ * 主机A向主机B发送“UDP-NetBIOS-NS”询问包,即向主机B的137端口,发Query包来询问主机B的NetBIOS Names信息。 @b@ * 其次,主机B接收到“UDP-NetBIOS-NS”询问包,假设主机B正确安装了NetBIOS服务........... 而且137端口开放,则主机B会向主机A发送一个“UDP-NetBIOS-NS”应答包,即发Answer包给主机A。 @b@ * 并利用UDP(NetBIOS Name Service)来快速获取远程主机MAC地址的方法 @b@ *  @b@ */  @b@public class UdpGetClientMacAddr {  @b@	@b@    private String sRemoteAddr;  @b@    private int iRemotePort=137;  @b@    private byte[] buffer = new byte[1024];  @b@    private DatagramSocket ds=null;  @b@  @b@    public UdpGetClientMacAddr(String strAddr) throws Exception{  @b@        sRemoteAddr = strAddr;  @b@        ds = new DatagramSocket();  @b@    }  @b@  @b@    public final DatagramPacket send(final byte[] bytes) throws IOException {  @b@        DatagramPacket dp = new DatagramPacket(bytes,bytes.length,InetAddress.getByName(sRemoteAddr),iRemotePort);  @b@        ds.send(dp);  @b@        return dp;  @b@    }  @b@  @b@    public final DatagramPacket receive() throws Exception {  @b@        DatagramPacket dp = new DatagramPacket(buffer,buffer.length);  @b@        ds.receive(dp);  @b@        return dp;  @b@    }  @b@    public byte[] GetQueryCmd() throws Exception {  @b@        byte[] t_ns = new byte[50];  @b@        t_ns[0] = 0x00;  @b@        t_ns[1] = 0x00;  @b@        t_ns[2] = 0x00;  @b@        t_ns[3] = 0x10;  @b@        t_ns[4] = 0x00;  @b@        t_ns[5] = 0x01;  @b@        t_ns[6] = 0x00;  @b@        t_ns[7] = 0x00;  @b@        t_ns[8] = 0x00;  @b@        t_ns[9] = 0x00;  @b@        t_ns[10] = 0x00;  @b@        t_ns[11] = 0x00;  @b@        t_ns[12] = 0x20;  @b@        t_ns[13] = 0x43;  @b@        t_ns[14] = 0x4B;  @b@  @b@        for(int i = 15; i < 45; i++){  @b@            t_ns[i] = 0x41;  @b@        }  @b@        t_ns[45] = 0x00;  @b@        t_ns[46] = 0x00;  @b@        t_ns[47] = 0x21;  @b@        t_ns[48] = 0x00;  @b@        t_ns[49] = 0x01;  @b@        return t_ns;  @b@    }  @b@    public final String GetMacAddr(byte[] brevdata) throws Exception {  @b@        // 获取计算机名  @b@        int i = brevdata[56] * 18 + 56;  @b@        String sAddr="";  @b@        StringBuffer sb = new StringBuffer(17);  @b@        // 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每个NetBIOS Names Info部分占18个字节)  @b@        // 然后可计算出“Unit ID”字段的位置=56+Number Of Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。  @b@        for(int j = 1; j < 7;j++)  @b@        {  @b@            sAddr = Integer.toHexString(0xFF & brevdata[i+j]);  @b@            if(sAddr.length() < 2)  @b@            {  @b@                sb.append(0);  @b@            }  @b@            sb.append(sAddr.toUpperCase());  @b@            if(j < 6) sb.append(':');  @b@        }  @b@        return sb.toString();  @b@    }  @b@  @b@    public final void close() throws Exception {  @b@        ds.close();  @b@    }  @b@  @b@    public final String GetRemoteMacAddr() throws Exception {  @b@        byte[] bqcmd = GetQueryCmd();  @b@        send(bqcmd);  @b@        DatagramPacket dp = receive();  @b@        String smac = GetMacAddr(dp.getData());  @b@        close();  @b@  @b@        return smac;  @b@    }  @b@      @b@    public static void main(String args[]) throws Exception{  @b@        UdpGetClientMacAddr umac=new UdpGetClientMacAddr("142.10.12.181");  @b@        umac=new UdpGetClientMacAddr("192.168.1.12");  @b@        System.out.println(umac.GetRemoteMacAddr());  @b@    }  @b@}

2. NetworkUtils 调用工具类(getMac方法)

package com.xwood.fs.util;@b@@b@import javax.servlet.http.HttpServletRequest;@b@@b@import org.apache.commons.lang3.StringUtils;@b@@b@public class NetworkUtils {@b@@b@	public static String getIp(HttpServletRequest request) {@b@		try {@b@			@b@			String ip = request.getHeader("x-forwarded-for");// 配置代理情况,配置代理后header信息通过x-forwarded-for标记真实ip地址@b@@b@			if ((ip == null) || (ip.length() == 0)@b@					|| ("unknown".equalsIgnoreCase(ip))) {@b@				ip = request.getHeader("Proxy-Client-IP");// 整合apache+Weblogic 代理情况@b@			}@b@@b@			if ((ip == null) || (ip.length() == 0)@b@					|| ("unknown".equalsIgnoreCase(ip))) {@b@				ip = request.getHeader("WL-Proxy-Client-IP");// 整合apache+Weblogic@b@																// 代理情况@b@			}@b@@b@			if ((ip == null) || (ip.length() == 0)@b@					|| ("unknown".equalsIgnoreCase(ip))) {@b@				ip = request.getRemoteAddr();// 获取未配置代理的情况@b@			}@b@@b@			ip = getIp(ip);@b@			return ip;@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@		return "";@b@	}@b@@b@	private static String getIp(String ip) {@b@		@b@		if ((ip != null) && (ip.indexOf(44) > 0)) {@b@			String[] ipArray = ip.split(",");@b@@b@			ip = ipArray[(ipArray.length - 1)].trim();@b@		}@b@		return ip;@b@		@b@	}@b@	@b@	@b@	public  static  String  getMac(HttpServletRequest request){@b@		try {@b@			String clientIp=getIp(request);@b@			if(StringUtils.isEmpty(clientIp)){@b@				return null;@b@			}@b@			UdpGetClientMacAddr umac = new UdpGetClientMacAddr(clientIp);@b@			return umac.GetRemoteMacAddr();@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@		return null;@b@	}@b@@b@}