浏览 3114 次
锁定老帖子 主题:根据IP获取Mac Address
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-23
最后修改:2010-01-23
Mac Address(Physical Address), 作为计算机的一个重要的标识,已经赢得很多的注册软件作为标识符号的青睐(尽管高级用户可以随意修改这个地址)。 下面是Java代码程序,没有经过更多平台的测试。使用时,请对平台进行更多测试.
如果你测试通过了X平台,或经过修改后成功取得了数据。请奉献出来或给予评论. /** * * @author ihavegotyou * Created on 2010-01-23 * Email ihavegotyou520 AT hotmail DOT com */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 根据ip获取MAC地址. * * @author ihavegotyou * @version 10.1.23 */ public class GetMacFromIp { public static String callCmd(String[] cmd) { String result = ""; String line = ""; try { Process proc = Runtime.getRuntime().exec(cmd); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null) { result += line; } } catch(Exception e) { e.printStackTrace(); } return result; } /** * * @param cmd 第一个命令 * @param another 第二个命令 * @return 第二个命令的执行结果 */ public static String callCmd(String[] cmd,String[] another) { String result = ""; String line = ""; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); int exitVal = proc.waitFor(); //已经执行完第一个命令,准备执行第二个命令 proc = rt.exec(another); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null) { result += line; } } catch(Exception e) { e.printStackTrace(); } return result; } /** * * @param ip 目标ip,一般在局域网内 * @param sourceString 命令处理的结果字符串 * @param macSeparator mac分隔符号 * @return mac地址,用上面的分隔符号表示 */ public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) { String result = ""; String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})"; Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(sourceString); while(matcher.find()){ result = matcher.group(1); if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) { break; //如果有多个IP,只匹配本IP对应的Mac. } } return result; } /** * * @param ip 目标ip * @return Mac Address * */ public static String getMacInWindows(final String ip){ String result = ""; String[] cmd = { "cmd", "/c", "ping " + ip }; String[] another = { "cmd", "/c", "arp -a" }; String cmdResult = callCmd(cmd,another); result = filterMacAddress(ip,cmdResult,"-"); return result; } /** * * @param ip 目标ip * @return Mac Address * */ public static String getMacInLinux(final String ip){ String result = ""; String[] cmd = { "/bin/sh", "-c", "ping " + ip + " -c 2 && arp -a" }; String cmdResult = callCmd(cmd); result = filterMacAddress(ip,cmdResult,":"); return result; } /** * 测试 */ public static void main(String[] args) { String fileSeparator = System.getProperty("file.separator"); //根据文件分隔符号区分平台 if (fileSeparator.equals("/")){ System.out.println(getMacInLinux("192.168.199.42")); } else if(fileSeparator.equals("\\")){ System.out.println(getMacInWindows("192.168.1.175")); //nicrosoft喜欢对着干 //System.out.println(getMacInWindows("127.0.0.1")); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |