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

获取Linux MAC地址

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

483
获取Linux MAC地址


//package com.nowjava;

import java.io.BufferedReader;

import java.io.IOException;
/*来自 
 nowjava.com - 时  代  Java*/

import java.io.InputStreamReader;


public class Main {

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

        System.out.println(getLinuxMACAddress());

    }


    public static String getLinuxMACAddress() {

        String mac = null;

        BufferedReader bufferedReader = null;

        Process process = null;/*来 自 n  o  w  j  a  v  a . c o m*/

        try {


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

            bufferedReader = new BufferedReader(new InputStreamReader(

                    process.getInputStream()));

            String line = null;

            int index = -1;

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

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


                if (index != -1) {

                    mac = line.substring(index + 4).trim();

                    break;

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (buff
展开阅读全文