Odczyt adresu MAC


Spis treści

     1 Wstęp
     2 Odczyt adresu MAC
          2.1 Interfejs i test
          2.2 Implementacja



Wstęp


MAC Address jest to unikalny numer interfejsu sieciowego pozwalający na jego identyfikację w sieci. Często utożsamia się go z numerem seryjnym urządzenia, co nie jest do końca poprawne, ale stanowi niezłe przybliżenie. Adresy MAC są unikalne w skali światowej.

Od wersji 1.4 można w javie odczytać adres MAC bez potrzeby pisania kodu w innym języku i użycia JNI.

Odczyt adresu MAC


Odczyt adresu MAC jest możliwy zarówno lokalnie jak i zdalnie.

Interfejs i test


Poniżej definicja interfejsu, który pozwala na odczyt adresu MAC za pomocą obiektu klasy InetAddress:

package pl.koziolekweb.programmers.macreader.MACReader;

import java.net.InetAddress;

public interface MACReader {

        byte[] getMacAddress(InetAddress inetAddress) throws SocketException;

        String getMacAddressAsString(InetAddress inetAddress) throws SocketException;

}


Poniżej test JUnit4 sprawdzający czy klasa działa poprawnie. Uwaga test, będzie działał poprawnie jeżeli zmienisz IP, MAC i MAC_BYTE na te, które ma twój komputer.

package pl.koziolekweb.programmers.macreader.MACReader;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class MACReaderTest {

        private final String IP = "10.203.1.73";
        private final String MAC = "00-21-70-B9-C1-5F";
        private final byte[] MAC_BYTE = new byte[] { 0, 33, 112, -71, -63, 95 };

        private MACReader reader;

        @Before
        public void before() {
                reader = new MACReaderImpl();
        }

        @Test
        public void getMacAddressAsStringTest() throws UnknownHostException, SocketException {
                String mac = reader.getMacAddressAsString(InetAddress.getLocalHost());
                Assert.assertEquals(MAC, mac);
                mac = reader.getMacAddressAsString(InetAddress.getByName(IP));
                Assert.assertEquals(MAC, mac);
        }

        @Test
        public void getMacAddressTest() throws UnknownHostException, SocketException {
                byte[] mac = reader.getMacAddress(InetAddress.getLocalHost());
                Assert.assertTrue(mac.length == 6);
                Assert.assertArrayEquals(MAC_BYTE, mac);

                mac = reader.getMacAddress(InetAddress.getByName(IP));
                Assert.assertTrue(mac.length == 6);
                Assert.assertArrayEquals(MAC_BYTE, mac);
        }
}


Implementacja


Na koniec klasa implementująca interfejs:

package pl.koziolekweb.programmers.macreader.MACReader;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Formatter;

public final class MACReaderImpl implements MACReader {

        public byte[] getMacAddress(InetAddress inetAddress) throws SocketException {
                NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress);
                byte[] mac = null;
                if (ni != null) {
                        mac = ni.getHardwareAddress();
                }
                return mac;
        }

        public String getMacAddressAsString(InetAddress inetAddress) throws SocketException, NullPointerException {
                Formatter formatter = new Formatter();
                byte[] mac = getMacAddress(inetAddress);
                if (mac != null) {
                        for (int i = 0; i < mac.length; i++) {
                                formatter.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                        }
                } else {
                        throw new NullPointerException("Adres niedostępny!");
                }

                return formatter.out().toString();
        }
}


Powyższy kod będzie działał od wersji 1.5 ze względu na użyty sposób formatowania wyników.
Informacje
Ostatnia modyfikacja 07-10-2009 11:53 Ostatni autor Koziołek
Ilość wyświetleń 2213 Wersja 1
Komentarz

Copyright © 2000-2006 by Coyote Group 0.9.3-pre3
Czas generowania strony: 0.1209 sek. (zapytań SQL: 9)