Getting a MAC address from the IP's retrieved from a ping sweep

1.2k Views Asked by At

Basically I have 2 bits of code one will do a ping sweep of a network range and then one will retrieve a MAC address from a given IP.

What I would like to do is incorporate these two pieces of code so when the ping sweep is performed it shows the MAC address in the output next to the IP addresses.

The MAC address retrieval code is as follows...

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package test118;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test118 {

public static String command;
public String ip = "192.168.0.4";

Test118() {


        command = "arp -a " + ip;


}

public void viewMac() {

    String process = null;
    String mac[] = new String[5];
    String rmac[] = new String[10];
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);
        InputStream inputstream = proc.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String line;
        int i = 0;
        while ((line = bufferedreader.readLine()) != null) {
            mac[i] = line;

            i++;
        }

        rmac = mac[3].split("    ");
        System.out.println(rmac[2]);
    } catch (Exception e) {
        System.out.println("mac cant find");
    }

}

public static void main(String[] args) throws Exception {

    Test118 r = new Test118();
    r.viewMac();
}
 }

The ping sweep is as follows...

package pingstestnew;

import java.io.IOException;
import java.net.InetAddress;

public class NetworkPing {


    public static void main(String[] args) throws IOException {

        InetAddress localhost = InetAddress.getLocalHost();
        // this code assumes IPv4 is used
        byte[] ip = localhost.getAddress();


        for (int i = 142; i <= 145; i++)
        {
            ip[3] = (byte)i;
            InetAddress address = InetAddress.getByAddress(ip);
        if (address.isReachable(1000))
        {
            System.out.println(address + " Host is reachable");
        }
        else if (!address.getHostAddress().equals(address.getHostName()))
        {
            System.out.println(address + " Hostname Resolved, Host is reachable");
        }
        else
        {
            System.out.println(address + " Host Unreachable");
        }
        }

    }
}
0

There are 0 best solutions below