集册 Java实例教程 获取Unix MAC地址

获取Unix MAC地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

505
获取Unix MAC地址


//package com.nowjava;

import java.io.BufferedReader;/** from NowJava.com**/

import java.io.IOException;

import java.io.InputStreamReader;


public class Main {

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

        System.out.println(getUnixMACAddress());

    }


    public static String getUnixMACAddress() {

        String mac = null;

        BufferedReader bufferedReader = null;

        Process process = null;

        try {

            process = Runtime.getRuntime().exec("ifconfig eth0");

            bufferedReader = new BufferedReader(new InputStreamReader(

                    process.getInputStream()));/*来自 时 代 J     a    v  a - nowjava.com*/

            String line = null;

            int index = -1;

            while ((line = bufferedReader.readLine()) != null) {

                index = line.toLowerCase().indexOf("hwaddr");

                if (index != -1) {

                    mac = line.substring(index + "hwaddr".length() + 1)

                            .trim();

                    break;

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                
展开阅读全文