• 获取客户机的ip和mac地址


    只获取clientIP

     1 package com.ppms.utils;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 
     5 /**
     6  * Created by liangyadong on 2018/9/6 0006.
     7  */
     8 public class GetClientIPUtil {
     9     
    10     public static String getIpAddr(HttpServletRequest request){
    11         String ip = request.getHeader("X-Forwarded-For");
    12         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
    13             ip = request.getHeader("Proxy-Client-IP");
    14         } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
    15             ip = request.getHeader("WL-Proxy-Client-IP");
    16         } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
    17             ip = request.getHeader("HTTP-CLIENT-IP");
    18         } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
    19             ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    20         } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
    21             ip = request.getRemoteAddr();
    22         }
    23         return ip;
    24     }
    25 }

    ================================================我是分割线.==================================================

    获取ip和mac地址:

    jsp:

    <%@ page import="com.ppms.utils.UdpGetClientMacAddr" %>
    <%
        String macAddress = "";
        String sip = request.getHeader("x-forwarded-for");
        if(sip == null || sip.length() == 0 || "unknown".equalsIgnoreCase(sip)) {
            sip = request.getHeader("Proxy-Client-IP");
        }
        if(sip == null || sip.length() == 0 || "unknown".equalsIgnoreCase(sip)) {
            sip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(sip == null || sip.length() == 0 || "unknown".equalsIgnoreCase(sip)) {
            sip = request.getRemoteAddr();
        }
        UdpGetClientMacAddr umac = new UdpGetClientMacAddr(sip);
        macAddress = umac.GetRemoteMacAddr();
    %>
    <input id="mac" type="hidden" value="<%=macAddress%>">

    java:

      1 package com.ppms.utils;
      2 
      3 import java.io.IOException;
      4 import java.io.InputStreamReader;
      5 import java.io.LineNumberReader;
      6 import java.net.DatagramPacket;
      7 import java.net.DatagramSocket;
      8 import java.net.InetAddress;
      9 
     10 /**
     11  * Created by liangyadong on 2018/8/6 0006.
     12  */
     13 public class UdpGetClientMacAddr {
     14     private String sRemoteAddr;
     15     private int iRemotePort=137;
     16     private byte[] buffer = new byte[1024];
     17     private DatagramSocket ds=null;
     18 
     19     public UdpGetClientMacAddr(String strAddr) throws Exception{
     20         sRemoteAddr = strAddr;
     21         ds = new DatagramSocket();
     22     }
     23 
     24     protected final DatagramPacket send(final byte[] bytes) throws IOException {
     25         DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName(sRemoteAddr),iRemotePort);
     26         ds.send(dp);
     27         return dp;
     28     }
     29 
     30     protected final DatagramPacket receive() throws Exception {
     31         DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
     32         ds.receive(dp);
     33         return dp;
     34     }
     35     // 询问包结构:
     36     // Transaction ID 两字节(16位) 0x00 0x00
     37     // Flags 两字节(16位) 0x00 0x10
     38     // Questions 两字节(16位) 0x00 0x01
     39     // AnswerRRs 两字节(16位) 0x00 0x00
     40     // AuthorityRRs 两字节(16位) 0x00 0x00
     41     // AdditionalRRs 两字节(16位) 0x00 0x00
     42     // Name:array [1..34] 0x20 0x43 0x4B 0x41(30个) 0x00 ;
     43     // Type:NBSTAT 两字节 0x00 0x21
     44     // Class:INET 两字节(16位)0x00 0x01
     45     protected byte[] GetQueryCmd() throws Exception {
     46         byte[] t_ns = new byte[50];
     47         t_ns[0] = 0x00;
     48         t_ns[1] = 0x00;
     49         t_ns[2] = 0x00;
     50         t_ns[3] = 0x10;
     51         t_ns[4] = 0x00;
     52         t_ns[5] = 0x01;
     53         t_ns[6] = 0x00;
     54         t_ns[7] = 0x00;
     55         t_ns[8] = 0x00;
     56         t_ns[9] = 0x00;
     57         t_ns[10] = 0x00;
     58         t_ns[11] = 0x00;
     59         t_ns[12] = 0x20;
     60         t_ns[13] = 0x43;
     61         t_ns[14] = 0x4B;
     62 
     63         for(int i = 15; i < 45; i++)
     64         {
     65             t_ns[i] = 0x41;
     66         }
     67 
     68         t_ns[45] = 0x00;
     69         t_ns[46] = 0x00;
     70         t_ns[47] = 0x21;
     71         t_ns[48] = 0x00;
     72         t_ns[49] = 0x01;
     73         return t_ns;
     74     }
     75     // 表1 “UDP-NetBIOS-NS”应答包的结构及主要字段一览表
     76     // 序号 字段名 长度
     77     // 1 Transaction ID 两字节(16位)
     78     // 2 Flags 两字节(16位)
     79     // 3 Questions 两字节(16位)
     80     // 4 AnswerRRs 两字节(16位)
     81     // 5 AuthorityRRs 两字节(16位)
     82     // 6 AdditionalRRs 两字节(16位)
     83     // 7 Name<Workstation/Redirector> 34字节(272位)
     84     // 8 Type:NBSTAT 两字节(16位)
     85     // 9 Class:INET 两字节(16位)
     86     // 10 Time To Live 四字节(32位)
     87     // 11 Length 两字节(16位)
     88     // 12 Number of name 一个字节(8位)
     89     // NetBIOS Name Info 18×Number Of Name字节
     90     // Unit ID 6字节(48位
     91     protected final String GetMacAddr(byte[] brevdata) throws Exception {
     92         // 获取计算机名
     93         int i = brevdata[56] * 18 + 56;
     94         String sAddr="";
     95         StringBuffer sb = new StringBuffer(17);
     96         // 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每个NetBIOS Names Info部分占18个字节)
     97         // 然后可计算出“Unit ID”字段的位置=56+Number Of Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。
     98         for(int j = 1; j < 7;j++)
     99         {
    100             sAddr = Integer.toHexString(0xFF & brevdata[i+j]);
    101             if(sAddr.length() < 2)
    102             {
    103                 sb.append(0);
    104             }
    105             sb.append(sAddr.toUpperCase());
    106 //            if(j < 6) sb.append(':');//用:分隔
    107             if(j < 6) sb.append('-');//用-分隔
    108         }
    109         return sb.toString();
    110     }
    111 
    112     public final void close() {
    113         try
    114         {
    115             ds.close();
    116         }
    117         catch (Exception ex){
    118             ex.printStackTrace();
    119         }
    120     }
    121 
    122     public final String GetRemoteMacAddr() throws Exception {
    123         byte[] bqcmd = GetQueryCmd();
    124         send(bqcmd);
    125         DatagramPacket dp = receive();
    126         String smac = GetMacAddr(dp.getData());
    127         close();
    128 
    129         return smac;
    130     }
    131 
    132     public String getMACAddress(String ip){
    133         String str = "";
    134         String macAddress = "";
    135         try {
    136             Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
    137             InputStreamReader ir = new InputStreamReader(p.getInputStream());
    138             LineNumberReader input = new LineNumberReader(ir);
    139             for (int i = 1; i < 100; i++) {
    140                 str = input.readLine();
    141                 if (str != null) {
    142                     if (str.indexOf("MAC Address") > 1) {
    143                         macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
    144                         break;
    145                     }
    146                 }
    147             }
    148         } catch (IOException e) {
    149             e.printStackTrace(System.out);
    150         }
    151         return macAddress;
    152     }
    153 
    154 }

    注意106和107两行的注释.

  • 相关阅读:
    (九)MySQL用户和权限管理
    activemq修改admin密码
    zookeeper与activemq整合
    (十一)数组array
    (十)while和until循环
    (八)MySQL索引操作
    查看MySQL是否在运行
    MySQL的启动和关闭
    常用的Web服务器软件整理
    [CodeForce 801A] Vicious Keyboard
  • 原文地址:https://www.cnblogs.com/yadongliang/p/9585453.html
Copyright © 2020-2023  润新知