0

関連するコンピューター (localhost を除く) のすべての IP アドレスと MAC ID をコンソールに返す Java コードがあります。各 IP と関連する MAC ID が新しい行に表示されます。IP1、MACID1、IP2、MACID2 など、この各行を新しい各変数に格納できますか? 解決待ち。前もって感謝します。

これが私のコードです:

import java.net.*;
import java.util.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class NIC {

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

    List<InetAddress> addrList = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> interfaces = null;
    try {
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
    }

    InetAddress localhost = null;

    try {
        localhost = InetAddress.getByName("127.0.0.1");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    while (interfaces.hasMoreElements()) {
        NetworkInterface ifc = interfaces.nextElement();
        Enumeration<InetAddress> addressesOfAnInterface = ifc.getInetAddresses();

        while (addressesOfAnInterface.hasMoreElements()) {
            InetAddress address = addressesOfAnInterface.nextElement();

            if (!address.equals(localhost) && !address.toString().contains(":")) {
                addrList.add(address);
                //System.out.println("\n");
                System.out.println(address.getHostAddress() + "\r");
                //System.out.println("\n");

try {
            //InetAddress address = InetAddress.getLocalHost();
            InetAddress address1 = InetAddress.getByName(address.getHostAddress());

            /*
             * Get NetworkInterface for the current host and then read
             * the hardware address.
             */
            NetworkInterface ni = 
                    NetworkInterface.getByInetAddress(address1);
            if (ni != null) {
                byte[] mac = ni.getHardwareAddress();
                if (mac != null) {
                    /*
                     * Extract each array of mac address and convert it 
                     * to hexa with the following format 
                     * 08-00-27-DC-4A-9E.
                     */
                    for (int i = 0; i < mac.length; i++) {

                        System.out.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n");
                    }
                } else {
                    System.out.println("Address doesn't exist or is not " +
                            "accessible.");
                }
            } else {
                System.out.println("Network Interface for the specified " +
                        "address is not found.");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
            }
        }
    }

}
}
4

2 に答える 2

1

マップを使ってみませんか?? その場合、IP アドレスと MAC ID を一緒に保存できます。

Map<String,String> addressMap = new HashMap<String,String>();

        String macStr = "";
        for (int i = 0; i < mac.length; i++) {
            System.out.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n");
            macStr += String.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n");
        }
        System.out.println("macStr" + macStr);
        addressMap.put(address.getHostAddress(), macStr);

反復マップ。

for (Map.Entry<String, String> entry : addressMap.entrySet())
        {
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }

コード全体

import java.net.*;
import java.util.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class NIC {

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

        List<InetAddress> addrList = new ArrayList<InetAddress>();
        Map<String,String> addressMap = new HashMap<String,String>();

        Enumeration<NetworkInterface> interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        InetAddress localhost = null;

        try {
            localhost = InetAddress.getByName("127.0.0.1");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        while (interfaces.hasMoreElements()) {
            NetworkInterface ifc = interfaces.nextElement();
            Enumeration<InetAddress> addressesOfAnInterface = ifc.getInetAddresses();

            while (addressesOfAnInterface.hasMoreElements()) {
                InetAddress address = addressesOfAnInterface.nextElement();

                if (!address.equals(localhost) && !address.toString().contains(":")) {
                    addrList.add(address);
                    //System.out.println("\n");
                    System.out.println(address.getHostAddress() + "\r");
                    //System.out.println("\n");

                    try {
                        //InetAddress address = InetAddress.getLocalHost();
                        InetAddress address1 = InetAddress.getByName(address.getHostAddress());

                        /*
                         * Get NetworkInterface for the current host and then read
                         * the hardware address.
                         */
                        NetworkInterface ni = 
                                NetworkInterface.getByInetAddress(address1);
                        if (ni != null) {
                            byte[] mac = ni.getHardwareAddress();
                            if (mac != null) {
                                /*
                                 * Extract each array of mac address and convert it 
                                 * to hexa with the following format 
                                 * 08-00-27-DC-4A-9E.
                                 */

                                String macStr = "";
                                for (int i = 0; i < mac.length; i++) {
                                    macStr += String.format("%02X%s",mac[i], (i < mac.length - 1) ? "-" : "" +"\n");
                                }
                                addressMap.put(address.getHostAddress(), macStr);
                            } else {
                                System.out.println("Address doesn't exist or is not " +
                                        "accessible.");
                            }
                        } else {
                            System.out.println("Network Interface for the specified " +
                                    "address is not found.");
                        }
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (SocketException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        for (Map.Entry<String, String> entry : addressMap.entrySet())
        {
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }
    }
}
于 2012-10-04T05:17:40.653 に答える
1

各 IP/MAC アドレスを ArrayList に追加するだけでよいはずです。完成したら、それらすべてを 1 か所にまとめます。

于 2012-10-04T04:40:30.480 に答える