1. 先使用ping -n  2 10.0.0.1 命令,如果返回的结果中含有TTL字符,证明ping 10.0.0.1是能ping通的,即可达的。如果在Linux机器上请使用 ping -c 2 10.0.0.1命令
  2. 再使用arp -a 10.0.0.1命令,在windows的cmd中使用该命令的返回结果如下图
  3. 得到 10.0.0.1的Mac地址为34-96-72-a0-ee-b5
  4. 既然通过这两个命令能实现从IP地址到MAC地址的转换,那么在Java后台无非就是执行这两条命令,对结果进行分析,对字符串进行处理得到MAC地址
  5. package com.security.common.utils;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern; public class IP2MacUtils {
    public static String commond(String cmd) throws IOException{
    Process process = Runtime.getRuntime().exec(cmd);
    InputStream inputStream = process.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder stringBuilder = new StringBuilder();
    String line = null;
    while((line = bufferedReader.readLine()) != null ){
    //String encode = System.getProperty("sun.jun.encoding");
    stringBuilder.append(line);
    }
    return stringBuilder.toString();
    } public static String getMacByIP(String IPAddress) throws IOException{
    String result = commond("ping -n 3 " + IPAddress);
    if ( result.contains("TTL")) {
    result = commond("arp -a " + IPAddress);
    }
    System.out.println(result);
    String regExp = "([0-9A-Fa-f]{2})([-:][0-9A-Fa-f]{2}){5}";
    Pattern pattern = Pattern.compile(regExp);
    Matcher matcher = pattern.matcher(result);
    StringBuilder stringBuilder = new StringBuilder();
    while( matcher.find() ){
    String temp = matcher.group();
    System.out.println(temp);
    stringBuilder.append(temp);
    }
    return stringBuilder.toString();
    } public static void main(String[] args) throws IOException{
    System.out.println("MAC : " + getMacByIP("10.0.0.1"));
    }
    }
  6. 最终程序的运行结果为
  7. 得到的结果一如命令行得到的结果,从而实现了Java中由IP地址得到MAC地址。

  8. Java.lang.Process , Pattern , Matcher ,正则表达式的简单使用

Java根据IP地址获取MAC地址的更多相关文章

  1. Java根据ip地址获取Mac地址,Java获取Mac地址

    Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...

  2. Windows下Java如何调用本地获取mac地址

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  3. java获取服务器IP地址及MAC地址的方法

    这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下   本文实例讲述了 ...

  4. JAVA获取客户端IP地址和MAC地址

    1.获取客户端IP地址 public String getIp(HttpServletRequest request) throws Exception { String ip = request.g ...

  5. java获取本机IP地址和MAC地址的方法

    // 获取ip地址 public static String getIpAddress() { try { Enumeration<NetworkInterface> allNetInte ...

  6. Java -- 获取MAC地址

    啦啦啦 package com.xindatai.common.util; import java.io.InputStream; import java.util.regex.Matcher; im ...

  7. android获取Mac地址和IP地址

    获取Mac地址实际项目中测试了如下几种方法:(1)设备开通Wifi连接,获取到网卡的MAC地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法) //根据Wifi信 ...

  8. 获取设备信息——获取客户端ip地址和mac地址

    1.获取本地IP(有可能是 内网IP,192.168.xxx.xxx) /** * 获取本地IP * * @return */ public static String getLocalIpAddre ...

  9. 使用Python获取计算机名,ip地址,mac地址等等

    获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...

随机推荐

  1. ubuntu boot空间不足的解决方法

    ubuntu boot空间不足的解决方法 2013年12月11日 11:11:39 yypony 阅读数:17000 标签: ubuntu内存boot内核更多 个人分类: linux_usageubu ...

  2. TF和SD

    TF卡又称T-Flash卡,全名:TransFLash,又名:Micro SD SD卡(Secure Digital Memory Card,安全数码卡)

  3. python中的列表生成式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, ...

  4. PHPExcel导出插入图片和居中问题

    首先到网上先下载PHPExcel 下载后解压得到这两个文件 下载后引用该文件 最后编写相关代码: 首先是图片插入导出 $objDrawing = new PHPExcel_Worksheet_Draw ...

  5. 兔子--android中百度地图的开发

    效果: API Key的申请地址:http://lbsyun.baidu.com/apiconsole/key 申请注意事项: 安全码:以下界面的SHA1  fingerprint值+;+包名 比如: ...

  6. Ubuntu 16.04 LTS 完善解决亮度调整

    环境: ubuntu16.04 lts acer aspire 4752G i5-2450M 内容来源:点击这里 ubuntu无法调整屏幕亮度,对笔记本来说很耗电,同时也很刺眼,因为它是默认以最大亮度 ...

  7. c#后台读写Cookie

    public class BaseCookies { #region Cookies public static void SetCookieValue(string key, string valu ...

  8. 剑指 offer set 26 不用加减乘除做加法

    总结 1. Leetcode 上有一道题, 是不用乘除做乘法, 那道题算是背包问题的变形 2. 不用加减乘除, 还可以用移位操作 3. 将数字转成二进制格式, 然后运用二进制亦或, 移位运算解决 3. ...

  9. 数据库为什么要用B+树结构--MySQL索引结构的实现(转)

    B+树在数据库中的应用 { 为什么使用B+树?言简意赅,就是因为: 1.文件很大,不可能全部存储在内存中,故要存储到磁盘上 2.索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数(为什么使用B-/ ...

  10. ORA-00972: 标识符过长

    若是拼接成的sql语句,请查找传递参数时字符型字段是否两边少了引号.