博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java -- 获取MAC地址
阅读量:7103 次
发布时间:2019-06-28

本文共 2313 字,大约阅读时间需要 7 分钟。

啦啦啦

package com.xindatai.common.util;import java.io.InputStream;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Lime {    public static void main(String[] args) throws Exception {        String mac = getMacAdderss("192.168.10.10");        System.out.println(mac);    }        /**     * 获取mac地址     *      * @author Liang     *     * 2017年4月26日     */    private static String getMacAdderss(String ip) throws Exception {        if(ping(ip)){            String result = command("arp -a " + ip);            String regExp = "([0-9A-Fa-f]{2})([-:][0-9A-Fa-f]{2}){5}";            Pattern pattern = Pattern.compile(regExp);            Matcher matcher = pattern.matcher(result);            StringBuilder mac = new StringBuilder();//            对字符串进行匹配,匹配到的字符串可以在任何位置            while(matcher.find()){//                返回匹配到的子字符串                String temp = matcher.group();                mac.append(temp);            }            return mac.toString();        }        return null;    }    /**     * ping ip     *      * @param ip     *      * @return     *      * @author Liang     *     * 2017年4月26日     */    private static boolean ping(String ip) throws Exception {        String os = getOsName();        String ping = "";        if(os.startsWith("Windows")){            ping = "ping " + ip + " -n 2";        }else if(os.startsWith("Linux")){            ping = "ping " + ip + " -c 2";        }        String result = command(ping);        if(result.contains("TTL") || result.contains("ttl")){            return true;        }        return false;    }    /**     * 执行单条指令     *      * @param cmd 命令     *      * @return 执行结果     *      * @author Liang     *     * 2017年4月26日     */    private static String command(String cmd) throws Exception {        Process process = Runtime.getRuntime().exec(cmd);        process.waitFor();        InputStream in = process.getInputStream();        StringBuilder result = new StringBuilder();        byte[] data = new byte[256];        while(in.read(data) != -1){//            操作系统中的编码方式            String encoding = System.getProperty("sun.jnu.encoding");            result.append(new String(data,encoding));        }        return result.toString();    }    private static String getOsName() {        return System.getProperty("os.name");    }}

啦啦啦

转载地址:http://wuchl.baihongyu.com/

你可能感兴趣的文章
我的博客地址和github地址
查看>>
最大公约数(gcd)还有最小公倍数(lcm)的共通之处
查看>>
2018.10.30-dtoj-4009-秀秀的森林(forest)
查看>>
[LeetCode] Strobogrammatic Number II
查看>>
《C语言》-(static和extern)
查看>>
HBase Snapshot简介
查看>>
//……关于TCP三次握手与四次挥手
查看>>
blocksit
查看>>
renderscript 浅析(一)
查看>>
弹出对话框---MessageBox
查看>>
定义的命令不可用的原因及解决办法
查看>>
libevent(十三)evhttp事件处理流程
查看>>
1004. 西西弗斯式的命运——java
查看>>
jquery弹出遮掩层
查看>>
前端基础-CSS
查看>>
14、BigInteger类简介
查看>>
Ubuntu14.04安装CUDA6.5
查看>>
python工程化最佳实践
查看>>
php正则表达式总结
查看>>
Jquery判断数组中是否包含某个元素$.inArray()的用法
查看>>