转载自:https://blog.csdn.net/chwshuang/article/details/46958719

叙述

我们都知道通过在电脑上敲 cmd 后,输入ipconfig -all 来获取IP地址或者MAC地址,java程序可以在window和linux下获取MAC地址等信息!

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;/*** * @author  http://zhidao.baidu.com/question/292753600.html*/
public class TestMac {public static void main(String[] args) {long start = System.currentTimeMillis();System.out.println("Operation System=" + getOsName());System.out.println("Mac Address=" + getMACAddress());System.out.println("通过ip获取mac地址为:" + getMACAddress("192.168.2.14"));long end = System.currentTimeMillis();System.out.println("获取电脑MAC地址时间为:" + (end - start));}/*** 获取操作系统名称*/public static String getOsName() {String os = "";os = System.getProperty("os.name");return os;}/*** 获取MAC地址*/public static String getMACAddress() {String address = "";String os = getOsName();//根据操作系统类型获取MAC地址if (os.startsWith("Windows")) {try {String command = "cmd.exe /c ipconfig /all";Process p = Runtime.getRuntime().exec(command);BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));String line;while ((line = br.readLine()) != null) {if (line.indexOf("Physical Address") > 0) {int index = line.indexOf(":");index += 2;address = line.substring(index);break;}}br.close();return address.trim();} catch (IOException e) {}} else if (os.startsWith("Linux")) {String command = "/bin/sh -c ifconfig -a";Process p;try {p = Runtime.getRuntime().exec(command);BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));String line;while ((line = br.readLine()) != null) {if (line.indexOf("HWaddr") > 0) {int index = line.indexOf("HWaddr") + "HWaddr".length();address = line.substring(index);break;}}br.close();} catch (IOException e) {}}address = address.trim();return address;}/*** 根据IP地址获取MAC地址*/public static String getMACAddress(String ipAddress) {String str = "", strMAC = "", macAddress = "";try {Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);InputStreamReader ir = new InputStreamReader(pp.getInputStream());LineNumberReader input = new LineNumberReader(ir);for (int i = 1; i < 100; i++) {str = input.readLine();if (str != null) {if (str.indexOf("MAC Address") > 1) {strMAC = str.substring(str.indexOf("MAC Address") + 14, str.length());break;}}}} catch (IOException ex) {return "Can't Get MAC Address!";}if (strMAC.length() < 17) {return "Error!";}macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) + ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) + ":"+ strMAC.substring(12, 14) + ":" + strMAC.substring(15, 17);return macAddress;}
}

注意:

这段代码需要公网访问的服务器上部署后有效,自己的机器上属于局域网,一般获取的都是本地IP,效果不同

JAVA程序 通过IP地址 获取MAC地址相关推荐

  1. Python根据IP地址获取MAC地址

    Python3根据IP地址获取MAC地址(不能获取本机IP,可以获取与本机同局域网设备IP的MAC) main.py #!/usr/bin/env python3 # -*- coding: utf- ...

  2. python通过ip获取mac地址_Python3根据IP地址获取MAC地址

    这是一种笨方法,是根据系统内置命令去获取的.我只是抛砖引玉,希望有人给我分享更好的代码,比如通过ARP协议去获取MAC地址? 1.[代码][Python]代码 #!/usr/bin/env pytho ...

  3. java web 获取 mac 地址_java web根据访问的ip地址获取MAC地址

    /** * 取客户端MAC地址 * @author huangwg 2014-06-26 */ public static String getMACAddress(HttpServletReques ...

  4. java 检查 mac 地址_使用Java在本地计算机上获取MAC地址

    有了我在这里找到的所有可能的解决方案以及其他答复,我将为我的解决方案做出贡献.您需要使用包含" ip"或" mac"的字符串来指定参数,具体取决于您要检查的内容 ...

  5. linux qt获取网卡mac地址,QT实战获取主机名QT获取IP地址 获取MAC地址 获取广播地址...

    QT的网络东西也蛮多的,我们今天先学一部分,然后后面在学一部分吧 一个能使用网络的应用,我们需要在项目工程中的pro文件里进行标记QT += core gui #标记 网络 QT += network ...

  6. python获取mac窗口坐标_[代码全屏查看]-Python3根据IP地址获取MAC地址

    [1].[代码] [Python]代码 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import platform import ...

  7. JAVA获取局域网内对应IP电脑的MAC地址(物理地址)

    创建类UdpGetClientMacAddr.java package com.shuzhiqiang.common;import java.io.IOException; import java.n ...

  8. java获取IP地址和MAC地址方式

    java获取IP地址和MAC地址方式 前端和后台都可以获取 ip获取方式比较简单,就不做分析了,主要说mac的获取 前端获取的方式与局限性:尝试好几种方式,这种是最易实现.最简单的方式,确实能获取该浏 ...

  9. 获取IP地址和MAC地址

    先获取IP地址,再通过IP地址获取MAC地址,Mark一下. //获得IP地址WSADATA wsaData;char name[155];char * ip;PHOSTENT hostinfo;if ...

最新文章

  1. Zabbix 3.0 部署监控 [三]
  2. 第三次学JAVA再学不好就吃翔(part51)--String类的转换功能
  3. 【Java】不正当使用break语句的危害
  4. Ruby1.9.3 Rails 3.2.3安装
  5. ios 开发需要看的书籍
  6. HTML+CSS+JS实现网页随机点名
  7. 年底跳槽,都去哪儿?数万从业者的新选择即将揭幕
  8. Qt基于FFmpeg读取摄像头并进行H264编码
  9. 常见距离算法-欧氏距离、杰卡德距离、余弦距离
  10. nsh 自定义启动脚本
  11. DataV(对象类)未来三天天气状态显示对应图标,格式:{ “results“:[ { “location“:{},“daily“:[ {},{}] ] }
  12. python实现模拟按键down_Python如何实现键盘鼠标按键模拟 Python实现键盘鼠标按键模拟代码...
  13. 【同步与补偿】频率偏移
  14. git 怎么切换分支命令_git命令-切换分支
  15. Blinker点灯科技绑定GitHub增加设备数量到10个
  16. 调试经验——键盘C键V键和H键失灵
  17. PL/SQL批量运行SQL语句
  18. Git “强制“提交代码
  19. Win11杜比全景声无法正常运行的解决方法教学
  20. NYOJ 118 次小生成树

热门文章

  1. 将数据库转换为word文档
  2. TensorFlow知网文章1-TensorFlow平台下的视频目标跟踪深度学习模型设计
  3. 毕业季快到了,在为毕业设计担心嘛?收下这份毕设知识大扫盲
  4. 【音视频】技术提升2.0
  5. 新商业模式:在区块链上寻找新机会
  6. robocup初学(第一篇)
  7. Java、JSP实验室预约管理系统
  8. c语言双精度小数点后取几位_c语言float类型小数点后位数
  9. fla文件中切记不能使用TLF文本
  10. APP接入友盟统计,不上报数据问题