• java得到clientIP地址和MAC住址


    最近的项目应该得到client的mac住址。

    服务器移植centos制,arm建筑箱。client手机和移动设备。(其他方案也应该是一流的似的)

    首先,要获得ip住址:

    依据client的http请求,利用request.getRemoteAddr()获取clientIp地址。在局域网内getRemoteAddr()和getRemoteHost()获得的结果同样。

    request.getRemoteAddr()是获得client的ip地址 。getRemoteHost()是获得client的主机名 。

    在有些场景中,可能有Squid等反向代理软件就不能获取到client的真实IP地址了。

    经过代理以后,因为在client和服务之间添加了中间层,因此server无法直接拿到client的IP,server端应用也无法直接通过转发请求的地址返回给client。可是在转发请求的HTTP头信息中,添加了X-FORWARDED-FOR信息。用以跟踪原有的clientIP地址和原来client请求的server地址。当我们訪问http://www.letu.com/index.jsp/ 时。事实上并非我们浏览器真正訪问到了server上的index.jsp文件,而是先由代理server去訪问http://159.226.94.32/index.jsp 。代理server再将訪问到的结果返回给我们的浏览器。由于是代理server去訪问index.jsp的,所以index.jsp中通过request.getRemoteAddr()的方法获取的IP实际上是代理server的地址,并非client的IP地址。

    //获取ip地址
    	public 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.getRemoteAddr();
    		}
    		return ip;
    	}

    获取mac地址:

    ARP命令用于显示和改动“地址解析协议(ARP)”缓存中的项目。ARP缓存中包括一个或多个表,它们用于存储IP地址及其经过解析的以太网或令牌环物理地址。

    计算机上安装的每个以太网或令牌环网络适配器都有自己单独的表。假设在没有參数的情况下使用。则ARP命令将显示帮助信息。仅仅有当TCP/IP协议在网络连接中安装为网络适配器属性的组件时,该命令才可用。另外netstat命令经常和ARP配合使用。netstat命令可以让用户得知眼下都有哪些网络连接正在运作。可以显示网络连接、路由表和网络接口信息。arp命令主要作用是显示和改动“地址解析协议 (ARP)”缓存 中的项目。 ARP 缓存中包括一个或多个表。它们用于存储 IP 地址及其经过解析的物理地址(MAC地址)。  计算机上安装的每个网卡都有自己单独的表。 假设在没有參数的情况下使用,则 arp 命令将显示帮助信息。 
    我们用ARP -n 列出具体信息。


    [root@centos-5 bin]# arp -n
    Address                  HWtype  HWaddress           Flags Mask            Iface
    159.226.94.126           ether   A4:56:30:D3:5D:43   C                     eth0
    获取mac的java 代码:
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    
    public class MACAddress {
        String ip;
        String mac;
        public MACAddress (String ip){
            this.ip = ip;
        }
        public MACAddress (){
            this.ip = "0.0.0.0";
        }
        public String getMac(){
            try {
    	    Process p = Runtime.getRuntime().exec("arp -n");
                InputStreamReader ir = new InputStreamReader(p.getInputStream());
                LineNumberReader input = new LineNumberReader(ir);
    			p.waitFor();
                boolean flag = true;
    			String ipStr = "(" + this.ip + ")";
                while(flag) {
                    String str = input.readLine();
                    if (str != null) {
    					if (str.indexOf(ipStr) > 1) {
                            int temp = str.indexOf("at");
                            this.mac = str.substring(
                            temp + 3, temp + 20);
                            break;
                        }
                    } else
                    flag = false;
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace(System.out);
            }
            return this.mac;
        }
        public void setIp(String ip){
            this.ip = ip;
        }
    
    }

    參考文档:http://blog.sina.com.cn/s/blog_6c87374b01019nwk.html

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    asp.net 添加引用类型自动变为GAC
    FPDFCJK.BIN下载(Foxit Reader中/日/韩CJK文字符支持包)
    Failed to access IIS metabase.
    Failed to access IIS metabase.
    判断用户计算机是否安装了sql server
    (Microsoft.Reporting.WebForms.ReportViewer) is not compatible with the type of control (Microsoft.Reporting.WebForms.ReportViewer)
    当你被利用的价值越来越小时,路会越来越窄.
    tomcat 6.0如何配置虚拟目录?tomcat 6.0 不能列目录?
    史上最高科技,Big Data奥运
    基于ping命令的网络故障排查方法
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4842086.html
Copyright © 2020-2023  润新知