JAvaサーバ側はipに基づいてクライアントmacアドレスを取得する

2862 ワード

//    
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;	
}