java服务器端根据ip获取客户端mac地址_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java服务器端根据ip获取客户端mac地址

java服务器端根据ip获取客户端mac地址

 2014/8/20 22:01:29  475900947  程序员俱乐部  我要评论(1)
  • 摘要://判断系统publicStringcheckSys(){stmt=conn.createStatement();Stringos=System.getProperty("os.name");if(os.startsWith("win")||os.startsWith("Win")){mac=getMacAdr(ip);}else{mac=getMacAdrLinux(ip);}returnmac;}//根据ip获取mac地址<windows>
  • 标签:
//判断系统
public String checkSys(){
        stmt = conn.createStatement();
        String os =System.getProperty("os.name");
        if(os.startsWith("win") || os.startsWith("Win")){
                mac = getMacAdr(ip);
        }else{
                mac = getMacAdrLinux(ip);
        }
        return mac;
}
//根据ip获取mac地址<windows>
public static String getMacAdr(String ip){
        String str = "";
        String macAddress = "";
        try{
                Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
                InputStreamReader ir = new InputStreamReader(p.getInputStream());
                LineNumberReader input = new LineNumberReader(ir);
                for (int i = 1; i < 100; i++){
                        str = input.readLine();
                        if (str != null){
                                // 英文环境下
                                if (str.indexOf("MAC Address") > 1){
                                macAddress=str.substring(str.indexOf("MAC Address")+14,str.length());                        
                                        break;
                                }
                                // 中文环境下
                                if (str.indexOf("MAC 地址") > 1){
                                        macAddress = str.substring(str.indexOf("MAC 地址") + 14, str.length());
                                        break;            
                                 }
                                }
                    }
        } catch (IOException e){
                e.printStackTrace(System.out);
                e.toString();
        }
        return macAddress;
}
//根据ip获取mac地址<linux>
public String getMacAdrLinux(String ip){
        String macAddress ="";
        Process p;
        String str = "";
        try {
                p = Runtime.getRuntime().exec("arp -a " + ip);
                InputStreamReader ir = new InputStreamReader(p.getInputStream());
                LineNumberReader input = new LineNumberReader(ir);
                for (int i = 1; i < 100; i++){
                        str = input.readLine();
                        if (str != null){
                                macAddress = str.substring(str.indexOf("at ")+3,str.indexOf("at ")+20);
                                System.out.println(macAddress);
                        }
                 }
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        return macAddress;
}
  • 相关文章
    网友 2015/1/29 16:11:28 发表

    傻逼样的。

    网友 2015/1/29 15:48:18 发表

    傻逼样的。

发表评论
用户名: 匿名