Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 366|回复: 0

[网络编程学习]java通过ip获取mac地址

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-3 00:02:03 | 显示全部楼层 |阅读模式
    java通过ip获取mac地址

    1. import java.io.BufferedReader;
    2. import java.io.InputStreamReader;
    3. import java.util.regex.Matcher;
    4. import java.util.regex.Pattern;
    5. /**
    6. * 获取MAC地址
    7. * @author sunlightcs
    8. * 2011-4-29
    9. * http://hi.juziku.com/sunlightcs/
    10. */
    11. public class GetMacAddress {
    12.      
    13.      public static String callCmd(String[] cmd) {  
    14.          String result = "";  
    15.          String line = "";  
    16.          try {  
    17.              Process proc = Runtime.getRuntime().exec(cmd);  
    18.              InputStreamReader is = new InputStreamReader(proc.getInputStream());  
    19.              BufferedReader br = new BufferedReader (is);  
    20.              while ((line = br.readLine ()) != null) {  
    21.              result += line;  
    22.              }  
    23.          }  
    24.          catch(Exception e) {  
    25.              e.printStackTrace();  
    26.          }  
    27.          return result;  
    28.      }
    29.      
    30.      
    31.         
    32.      /**
    33.       *
    34.       * @param cmd  第一个命令
    35.       * @param another 第二个命令
    36.       * @return   第二个命令的执行结果
    37.       */  
    38.      public static String callCmd(String[] cmd,String[] another) {  
    39.          String result = "";  
    40.          String line = "";  
    41.          try {  
    42.              Runtime rt = Runtime.getRuntime();  
    43.              Process proc = rt.exec(cmd);  
    44.              proc.waitFor();  //已经执行完第一个命令,准备执行第二个命令  
    45.              proc = rt.exec(another);  
    46.              InputStreamReader is = new InputStreamReader(proc.getInputStream());  
    47.              BufferedReader br = new BufferedReader (is);  
    48.              while ((line = br.readLine ()) != null) {  
    49.                  result += line;  
    50.              }  
    51.          }  
    52.          catch(Exception e) {  
    53.              e.printStackTrace();  
    54.          }  
    55.          return result;  
    56.      }
    57.      
    58.      
    59.      
    60.      /**
    61.       *
    62.       * @param ip  目标ip,一般在局域网内
    63.       * @param sourceString 命令处理的结果字符串
    64.       * @param macSeparator mac分隔符号
    65.       * @return  mac地址,用上面的分隔符号表示
    66.       */  
    67.      public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) {  
    68.          String result = "";  
    69.          String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";  
    70.          Pattern pattern = Pattern.compile(regExp);  
    71.          Matcher matcher = pattern.matcher(sourceString);  
    72.          while(matcher.find()){  
    73.              result = matcher.group(1);  
    74.              if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) {  
    75.                  break;  //如果有多个IP,只匹配本IP对应的Mac.  
    76.              }  
    77.          }
    78.    
    79.          return result;  
    80.      }
    81.      
    82.      
    83.      
    84.      /**
    85.       *
    86.       * @param ip 目标ip
    87.       * @return   Mac Address
    88.       *
    89.       */  
    90.      public static String getMacInWindows(final String ip){  
    91.          String result = "";  
    92.          String[] cmd = {  
    93.                  "cmd",  
    94.                  "/c",  
    95.                  "ping " +  ip  
    96.                  };  
    97.          String[] another = {  
    98.                  "cmd",  
    99.                  "/c",  
    100.                  "arp -a"  
    101.                  };  
    102.    
    103.          String cmdResult = callCmd(cmd,another);  
    104.          result = filterMacAddress(ip,cmdResult,"-");  
    105.    
    106.          return result;  
    107.      }  

    108.      /**
    109.      *
    110.      * @param ip 目标ip
    111.      * @return   Mac Address
    112.      *
    113.      */  
    114.      public static String getMacInLinux(final String ip){  
    115.          String result = "";  
    116.          String[] cmd = {  
    117.                  "/bin/sh",  
    118.                  "-c",  
    119.                  "ping " +  ip + " -c 2 && arp -a"  
    120.                  };  
    121.          String cmdResult = callCmd(cmd);  
    122.          result = filterMacAddress(ip,cmdResult,":");  
    123.    
    124.          return result;  
    125.      }  
    126.      
    127.      /**
    128.       * 获取MAC地址
    129.       * @return 返回MAC地址
    130.       */
    131.      public static String getMacAddress(String ip){
    132.          String macAddress = "";
    133.          macAddress = getMacInWindows(ip).trim();
    134.          if(macAddress==null||"".equals(macAddress)){
    135.              macAddress = getMacInLinux(ip).trim();
    136.          }
    137.          return macAddress;
    138.      }
    139.         
    140.      /**
    141.      * 测试
    142.      */  
    143.      public static void main(String[] args) {           
    144.          System.out.println(getMacAddress("220.181.111.147"));
    145.      }
    146.    
    147. }
    复制代码
      


    源码下载:http://file.javaxxz.com/2014/11/3/000203609.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-11-12 15:09 , Processed in 0.653681 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表