title: Get All IP and Mac Address in lan
body: <p>I am listing ip and mac address all devices in lan (like network scanner)</p>
  
  <p>I want use java programming languages.</p>
  
  <p>If i use my ip address,result is true but if i use another ip address in lan,network variable is null.</p>
  
  <p>(for example ; my ip address : 192.168.1.7 , another ip address : 192.168.1.8)  </p>
  
  <p>Here my code;</p>
  
  <pre><code>public static void checkHosts(String subnet) throws UnknownHostException, IOException{
         int timeout=3000;
         for (int i=1;i&lt;255;i++){
             String host=subnet + "." + i;
             if (InetAddress.getByName(host).isReachable(timeout)){
                  System.out.println(host + " is reachable" + InetAddress.getByName(host));
  
  
                  NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
  
                  if(network!=null){
                      System.out.println(network.isUp());
                      byte[] mac = network.getHardwareAddress();
                      System.out.println(network.getDisplayName());
                      System.out.println(network.getName());
                      System.out.println(InetAddress.getByName(host).getHostName());
                      System.out.print("Current MAC address : ");
  
                      StringBuilder sb = new StringBuilder();
                      for (int j = 0; j &lt; mac.length; j++) {
                          sb.append(String.format("%02X%s", mac[j], (j &lt; mac.length - 1) ? "-" : ""));        
                      }
                      System.out.println(sb.toString());
                  }
  
             }
         }
      }
  </code></pre>