首页 技术 正文
技术 2022年11月15日
0 收藏 663 点赞 4,687 浏览 1564 个字

啦啦啦

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

啦啦啦

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295