JSP经过udp得到客服端MAC地址

 在网上看到的,感受挺实用的就转过来。java

  
  
  
  
  1. package com.sun.servlet; 
  2.  
  3. import java.io.IOException; 
  4. import java.net.DatagramPacket; 
  5. import java.net.DatagramSocket; 
  6. import java.net.InetAddress; 
  7. /** 
  8.  * 主机A向主机B发送“UDP-NetBIOS-NS”询问包,即向主机B的137端口,发Query包来询问主机B的NetBIOS Names信息。 
  9.  * 其次,主机B接收到“UDP-NetBIOS-NS”询问包,假设主机B正确安装了NetBIOS服务........... 并且137端口开放,则主机B会向主机A发送一个“UDP-NetBIOS-NS”应答包,即发Answer包给主机A。 
  10.  * 并利用UDP(NetBIOS Name Service)来快速获取远程主机MAC地址的方法 
  11.  *  
  12.  * @author WINDY 
  13.  */ 
  14. public class UdpGetClientMacAddr { 
  15.     private String sRemoteAddr; 
  16.     private int iRemotePort=137
  17.     private byte[] buffer = new byte[1024]; 
  18.     private DatagramSocket ds=null
  19.  
  20.     public UdpGetClientMacAddr(String strAddr) throws Exception{ 
  21.         sRemoteAddr = strAddr
  22.         ds = new DatagramSocket(); 
  23.     } 
  24.  
  25.     protected final DatagramPacket send(final byte[] bytes) throws IOException { 
  26.         DatagramPacket dp = new DatagramPacket(bytes,bytes.length,InetAddress.getByName(sRemoteAddr),iRemotePort); 
  27.         ds.send(dp); 
  28.         return dp; 
  29.     } 
  30.  
  31.     protected final DatagramPacket receive() throws Exception { 
  32.         DatagramPacket dp = new DatagramPacket(buffer,buffer.length); 
  33.         ds.receive(dp); 
  34.         return dp; 
  35.     } 
  36.     protected byte[] GetQueryCmd() throws Exception { 
  37.         byte[] t_ns = new byte[50]; 
  38.         t_ns[0] = 0x00; 
  39.         t_ns[1] = 0x00; 
  40.         t_ns[2] = 0x00; 
  41.         t_ns[3] = 0x10; 
  42.         t_ns[4] = 0x00; 
  43.         t_ns[5] = 0x01; 
  44.         t_ns[6] = 0x00; 
  45.         t_ns[7] = 0x00; 
  46.         t_ns[8] = 0x00; 
  47.         t_ns[9] = 0x00; 
  48.         t_ns[10] = 0x00; 
  49.         t_ns[11] = 0x00; 
  50.         t_ns[12] = 0x20; 
  51.         t_ns[13] = 0x43; 
  52.         t_ns[14] = 0x4B; 
  53.  
  54.         for(int i = 15; i < 45; i++){ 
  55.             t_ns[i] = 0x41; 
  56.         } 
  57.         t_ns[45] = 0x00; 
  58.         t_ns[46] = 0x00; 
  59.         t_ns[47] = 0x21; 
  60.         t_ns[48] = 0x00; 
  61.         t_ns[49] = 0x01; 
  62.         return t_ns; 
  63.     } 
  64.     protected final String GetMacAddr(byte[] brevdata) throws Exception { 
  65.         // 获取计算机名 
  66.         int i = brevdata[56] * 18 + 56; 
  67.         String sAddr=""
  68.         StringBuffer sb = new StringBuffer(17); 
  69.         // 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每一个NetBIOS Names Info部分占18个字节) 
  70.         // 而后可计算出“Unit ID”字段的位置=56+Number Of Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。 
  71.         for(int j = 1; j < 7;j++) 
  72.         { 
  73.             sAddr = Integer.toHexString(0xFF & brevdata[i+j]); 
  74.             if(sAddr.length() < 2
  75.             { 
  76.                 sb.append(0); 
  77.             } 
  78.             sb.append(sAddr.toUpperCase()); 
  79.             if(j < 6) sb.append(':'); 
  80.         } 
  81.         return sb.toString(); 
  82.     } 
  83.  
  84.     public final void close() { 
  85.         try 
  86.         { 
  87.             ds.close(); 
  88.         } 
  89.         catch (Exception ex){ 
  90.             ex.printStackTrace(); 
  91.         } 
  92.     } 
  93.  
  94.     public final String GetRemoteMacAddr() throws Exception { 
  95.         byte[] bqcmd = GetQueryCmd(); 
  96.         send(bqcmd); 
  97.         DatagramPacket dp = receive(); 
  98.         String smac = GetMacAddr(dp.getData()); 
  99.         close(); 
  100.  
  101.         return smac; 
  102.     } 

JSP文件:web

  
  
  
  
  1. <span style="color:#000000;"><
  2.                 String smac = ""
  3.                 String sip = request.getHeader("x-forwarded-for"); 
  4.                 if (sip == null || sip.length() == 0 
  5.                         || "unknown".equalsIgnoreCase(sip)) { 
  6.                     sip = request.getHeader("Proxy-Client-IP"); 
  7.                 } 
  8.                 if (sip == null || sip.length() == 0 
  9.                         || "unknown".equalsIgnoreCase(sip)) { 
  10.                     sip = request.getHeader("WL-Proxy-Client-IP"); 
  11.                 } 
  12.                 if (sip == null || sip.length() == 0 
  13.                         || "unknown".equalsIgnoreCase(sip)) { 
  14.                     sip = request.getRemoteAddr(); 
  15.                 } 
  16.                 UdpGetClientMacAddr umac = new UdpGetClientMacAddr(sip); 
  17.                 smac = umac.GetRemoteMacAddr(); 
  18.                 session.setAttribute("smac", smac); 
  19.             %></span>
相关文章
相关标签/搜索