Java: Get IP and Mac address

Veröffentlicht von

A small code snippet that reads the computers IP and MAC address:

try {
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network;
    network = NetworkInterface.getByInetAddress(ip);
    byte[] macaddr = network.getHardwareAddress();

    String mac = "";

    for (int i = 0; i < macaddr.length; i++) {
        mac += String.format("%02X%s", macaddr[i], (i < macaddr.length - 1) ? "-" : "");    
    }

    System.out.println("IP-Address: " + ip.toString());
    System.out.println("Mac-Address: " + mac);

} catch (SocketException | UnknownHostException e) {
    e.printStackTrace();
}

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.