java工具類,在Windows,Linux系統擷取電腦的MAC地址、本地IP、電腦名

來源:互聯網
上載者:User

標籤:puts   sdn   input   通過   rip   用戶端   tostring   for   reserve   

package com.cloudssaas.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/********************************************************************************* * //* Copyright (C) 2014 ××××××××××. All Rights Reserved. //* //* Filename: * ComputerInfo.java //* Revision: 1.0 //* Author: <yao xiucai> //* Created On: * 2014年5月21日 //* Modified by: //* Modified On: //* //* Description: *  * <取網卡物理地址-- * 1.在Windows,Linux系統下均可用; * 2.通過ipconifg,ifconfig獲得電腦資訊; * 3.再用模式比對方式尋找MAC地址,與作業系統的語言無關> *  * //* Description: <取電腦名稱--從環境變數中取> * abstract 限制繼承/建立執行個體 *//********************************************************************************/public abstract class ComputerInfo {    private static String macAddressStr = null;    private static String computerName = System.getenv().get("COMPUTERNAME");    private static final String[] windowsCommand = { "ipconfig", "/all" };    private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };    private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",            Pattern.CASE_INSENSITIVE);    /**     * 擷取多個網卡地址     *      * @return     * @throws IOException     */    private final static List<String> getMacAddressList() throws IOException {        final ArrayList<String> macAddressList = new ArrayList<String>();        final String os = System.getProperty("os.name");        final String command[];        if (os.startsWith("Windows")) {            command = windowsCommand;        } else if (os.startsWith("Linux")) {            command = linuxCommand;        } else {            throw new IOException("Unknow operating system:" + os);        }        // 執行命令        final Process process = Runtime.getRuntime().exec(command);        BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));        for (String line = null; (line = bufReader.readLine()) != null;) {            Matcher matcher = macPattern.matcher(line);            if (matcher.matches()) {                macAddressList.add(matcher.group(1));                // macAddressList.add(matcher.group(1).replaceAll("[-:]",                // ""));//去掉MAC中的“-”            }        }        process.destroy();        bufReader.close();        return macAddressList;    }    /**     * 擷取一個網卡地址(多個網卡時從中擷取一個)     *      * @return     */    public static String getMacAddress() {        if (macAddressStr == null || macAddressStr.equals("")) {            StringBuffer sb = new StringBuffer(); // 存放多個網卡地址用,目前只取一個非0000000000E0隧道的值            try {                List<String> macList = getMacAddressList();                for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {                    String amac = iter.next();                    if (!amac.equals("0000000000E0")) {                        sb.append(amac);                        break;                    }                }            } catch (IOException e) {                e.printStackTrace();            }            macAddressStr = sb.toString();        }        return macAddressStr;    }    /**     * 擷取電腦名     *      * @return     */    public static String getComputerName() {        if (computerName == null || computerName.equals("")) {            computerName = System.getenv().get("COMPUTERNAME");        }        return computerName;    }    /**     * 擷取用戶端IP地址     *      * @return     */    public static String getIpAddrAndName() throws IOException {        return InetAddress.getLocalHost().toString();    }    /**     * 擷取用戶端IP地址     *      * @return     */    public static String getIpAddr() throws IOException {        return InetAddress.getLocalHost().getHostAddress().toString();    }    /**     * 擷取電腦唯一標識     *      * @return     */    public static String getComputerID() {        String id = getMacAddress();        if (id == null || id.equals("")) {            try {                id = getIpAddrAndName();            } catch (IOException e) {                e.printStackTrace();            }        }        return computerName;    }    /**     * 限制建立執行個體     */    private ComputerInfo() {    }    public static void main(String[] args) throws IOException {        System.out.println(ComputerInfo.getMacAddress());        System.out.println(ComputerInfo.getComputerName());        System.out.println(ComputerInfo.getIpAddr());        System.out.println(ComputerInfo.getIpAddrAndName());    }}

 

相關:

 java中得到電腦MAC網卡標識,IP,電腦名稱等唯一標識問題

java工具類,在Windows,Linux系統擷取電腦的MAC地址、本地IP、電腦名

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.