Java Mac Address Get getMacAddress(String host)

Here you can find the source of getMacAddress(String host)

Description

get Mac Address

License

Apache License

Declaration

@SuppressWarnings("unused")
    public String getMacAddress(String host) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

public class Main {

    public static String getMacAddress() {
        String mac = "";
        String line = "";

        String os = System.getProperty("os.name");

        if (os != null && os.startsWith("Windows")) {
            try {
                String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);

                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

                while ((line = br.readLine()) != null) {
                    if (line.indexOf("Physical Address") > 0) {
                        int index = line.indexOf(":") + 2;

                        mac = line.substring(index);

                        break;
                    }//  w  w  w  .j  av a  2s. c o  m
                }

                br.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return mac;
    }

    @SuppressWarnings("unused")
    public String getMacAddress(String host) {
        String mac = "";
        StringBuffer sb = new StringBuffer();

        try {
            NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        mac = sb.toString();
        mac = mac.substring(0, mac.length() - 1);
        return mac;
    }
}

Related

  1. getMacAddress()
  2. getMacAddress()
  3. getMacAddress()
  4. getMacAddress()
  5. getMACAddress()
  6. getMACAddress(String interfaceName)
  7. getMacAddress(String separator)
  8. getMacAddressBytes()
  9. getMacAddresses()