从ping扫描中获取的IP获取MAC地址

时间:2013-12-16 12:35:18

标签: ping mac-address

基本上我有2位代码,一位将执行ping sweep网络范围,然后一位将从给定的 IP 中检索 MAC地址

我想要做的是合并这两段代码,以便在执行ping sweep时,它会在IP地址旁边的输出中显示 MAC地址

MAC地址检索代码如下......

/*
 * 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();
}
 }

ping sweep如下......

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 个答案:

没有答案