根据ip获取MAC

根据ip获取MAC

private String getKey(String ip) {
    try {
        Scanner s = new Scanner(Runtime.getRuntime().exec("arp -a " + a).getInputStream()).useDelimiter("\\A");
        if (s.hasNext()) {
            String source = s.next();
            Pattern pattern = Pattern.compile("([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})");
            Matcher matcher = pattern.matcher(source);
            if(matcher.find()) {
                log.debug("ip={} mac={}", ip, matcher.group(0));
                return matcher.group(0);
            }
            return "";
        } else {
            return "";
        }
    } catch (Exception e) {
        return "";
    }
}

你可能感兴趣的:(Java)