zoukankan      html  css  js  c++  java
  • Java -- 获取MAC地址

    啦啦啦

    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");
        }
    }

    啦啦啦

  • 相关阅读:
    centos yum 安装php7.2
    Linux CentOS完全卸载PHP
    Linux: cp 复制文件、文件夹到文件夹
    CentOS 7 yum安装LAMP,LNMP并搭建WordPress个人博客网站
    cin循环输入控制问题
    有序数组中的二分查找
    二叉查找树中元素的删除操作
    如何生成能在没有安装opencv库及vs2010环境的电脑上运行的exe文件
    冒泡排序算法,选择排序算法,插入排序算法
    使用迭代法穷举1到N位最大的数
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6769090.html
Copyright © 2011-2022 走看看