oguzpamuk
oguzpamuk

Reputation: 78

Get All IP and Mac Address in lan

I am listing ip and mac address all devices in lan (like network scanner)

I want use java programming languages.

If i use my ip address,result is true but if i use another ip address in lan,network variable is null.

(for example ; my ip address : 192.168.1.7 , another ip address : 192.168.1.8)

Here my code;

public static void checkHosts(String subnet) throws UnknownHostException, IOException{
       int timeout=3000;
       for (int i=1;i<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 < mac.length; j++) {
                        sb.append(String.format("%02X%s", mac[j], (j < mac.length - 1) ? "-" : ""));        
                    }
                    System.out.println(sb.toString());
                }

           }
       }
    }

Upvotes: 1

Views: 5735

Answers (4)

Vaishali Vijay
Vaishali Vijay

Reputation: 21

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RealMacAddress {

    public static void main(String args[]){
        try {
            Process proc = Runtime.getRuntime().exec("arp -a ");

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

            // read the output from the command
            String s = null;

            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);

            // read any errors from the attempted command
            while ((s = stdError.readLine()) != null) {
                System.err.println(s);
            }
            }}catch (IOException ex) {
            System.err.println(ex);
        }   

    }
}

Upvotes: 2

Anthony Collier
Anthony Collier

Reputation: 46

I've been working on a project to do the same thing. I think the best way to go about this is to execute another process at run time and read the results. As already suggested, you could read the system ARP table and parse results, but this is platform dependent. The windows command in command prompt is: arp -a.

I chose to make a remote call to nmap and parse those results. It requires installing nmap on your machine, but the solutions "should" be cross-platform as long as the appropriate version of nmap is installed:

Available here: https://nmap.org/download.html

Here's a quick example. You'd of course need to make some changes to dynamically choose the network to scan and parse the results instead of print them.

try {
    Process proc = Runtime.getRuntime().exec("nmap -PR -sn 192.168.1.0/24");

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

    // read the output from the command
    String s = null;

    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);

    // read any errors from the attempted command
    while ((s = stdError.readLine()) != null) {
        System.err.println(s);
    }
} catch (IOException ex) {
    System.err.println(ex);
}

Upvotes: 1

Xvolks
Xvolks

Reputation: 2155

The NetworkInterface class as stated in the comments (and javadoc) is for local interfaces. It cannot be used to identify remote NICs.

Upvotes: 1

Garuno
Garuno

Reputation: 2200

Basically, what you're trying to do is implementing a ping sweep on your local network. The code you provided is probably doing the desired ping sweep(implementation dependent), but it is only showing the MAC address of your local interface. To determine the MAC addresses of the computers on the network you have to look into the ARP cache of your machine which is not platform independent and therefore not easily doable in Java.

Upvotes: 2

Related Questions