2 Replies Latest reply: Sep 23, 2012 6:27 PM by EJP RSS

    How to get Mac Address of Remote Computer

    UdayaShankaraGandhi
      Hi All,

      I tried to find out a solution for finding Mac Address of a remote system, then first i tried with finding it with local address using the following code it works fine for me.
      public class MacAddressFinder {
      
           public static void main(String[] args) {
                try 
                {
                     InetAddress ipAddr = InetAddress.getLocalHost();
                     System.out.println("Current IP address : " + ipAddr.getHostAddress());
                     
                     NetworkInterface nwIntf;
                     try 
                     {
                          nwIntf = NetworkInterface.getByInetAddress(ipAddr);
                          byte[] hwAddr = nwIntf.getHardwareAddress();
                          
                          System.out.print("Current MAC address : ");
                          
                          StringBuilder sb = new StringBuilder();
                          for (int i = 0; i < hwAddr.length; i++) 
                          {
                               sb.append(String.format("%02X%s", hwAddr, (i < hwAddr.length - 1) ? "-" : ""));          
                          }
                          System.out.println(sb);
                     } catch (SocketException e) {
                          e.printStackTrace();
                     }
                }
                catch (UnknownHostException e) {
                     e.printStackTrace();
                }
           }
      }


      But when i tried this piece of code in jsp with the following code i am getting NeworkInterface object value as null and there by i am unable to get the Mac Address of a remote computer.
      InetAddress ipAddr = InetAddress.getByName(request.getRemoteAddr());
      NetworkInterface nwIntf = NetworkInterface.getByInetAddress(ipAddr);
      Can you please suggest me how to find out the Mac address of remote system?

      Thanks,
      Uday