/**
     *获取linux Ubuntu网卡的mac地址
     */
    public static  String  getmacAddressByLinux(){
        String mac = "";
        String[] commands = new String[]{"/bin/bash", "-c", "ifconfig eth0|grep 'HWaddr'|awk -F ' ' '{print $5}'"};
        try {
              mac =RmtShellExecutor.execArrayCmdRetrun("commands", commands);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mac;
    }
 

/**
     *  获取linux Ubuntu eth0 内网ip地址
     */
    public static String getinternalAddressByLinux(){
        String ip ="";
        String[] commands = new String[]{"/bin/bash", "-c", "ifconfig eth0|grep 'inet addr'|awk -F  ':'  '{print $2}'|awk '{print $1}'"};
        try {
           ip =RmtShellExecutor.execArrayCmdRetrun("commands", commands);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }
 

/**
     * 
     * @param message
     * @param args
     * @return
     * @throws Exception
     */
    public static String execArrayCmdRetrun(String message, String[] args) throws Exception {
        log.debug("---执行linux shell ---");
        log.debug(message + ":");
        String result = "";
        Process process = Runtime.getRuntime().exec(args);
        for (String arg : args) {
            System.out.println(arg);
            System.out.print(" ");
        }
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String line = null;
        while ((line = errorReader.readLine()) != null) {
            System.out.println("--errorReader--:"+line);
        }
        errorReader.close();
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = infoReader.readLine()) != null) {
            System.out.println("--infoReader--:"+line);
            result = line;
        }
        infoReader.close();
        log.debug("");
        return result;
    }
 

/**
     * 获取widnows网卡的mac地址.
     *
     * @return mac地址
     */
    public static String getWindowsMACAddress() {
        String mac = null;
        BufferedReader bufferedReader = null;
        Process process = null;
        try {
            // windows下的命令,显示信息中包含有mac地址信息
            process = Runtime.getRuntime().exec("ipconfig /all");
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line = null;
            int index = -1;
            while ((line = bufferedReader.readLine()) != null) {
                // 寻找标示字符串[physical
                index = line.toLowerCase().indexOf("physical address");

                if (index >= 0) {// 找到了
                    index = line.indexOf(":");// 寻找":"的位置
                    if (index >= 0) {
                        // 取出mac地址并去除2边空格
                        mac = line.substring(index + 1).trim();
                    }
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            bufferedReader = null;
            process = null;
        }

        return mac;
    }

    /**
     * windows 7 专用 获取MAC地址
     *
     * @return
     *
     * @throws Exception
     */
    public static String getMACAddress() throws Exception {

        // 获取本地IP对象
        InetAddress ia = InetAddress.getLocalHost();
        // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

        // 下面代码是把mac地址拼装成String
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            // mac[i] & 0xFF 是为了把byte转化为正整数
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length() == 1 ? 0 + s : s);
        }

        // 把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }
 

Logo

更多推荐