JAVA程序 通过IP地址 获取MAC地址
转载自: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地址相关推荐
- Python根据IP地址获取MAC地址
Python3根据IP地址获取MAC地址(不能获取本机IP,可以获取与本机同局域网设备IP的MAC) main.py #!/usr/bin/env python3 # -*- coding: utf- ...
- python通过ip获取mac地址_Python3根据IP地址获取MAC地址
这是一种笨方法,是根据系统内置命令去获取的.我只是抛砖引玉,希望有人给我分享更好的代码,比如通过ARP协议去获取MAC地址? 1.[代码][Python]代码 #!/usr/bin/env pytho ...
- java web 获取 mac 地址_java web根据访问的ip地址获取MAC地址
/** * 取客户端MAC地址 * @author huangwg 2014-06-26 */ public static String getMACAddress(HttpServletReques ...
- java 检查 mac 地址_使用Java在本地计算机上获取MAC地址
有了我在这里找到的所有可能的解决方案以及其他答复,我将为我的解决方案做出贡献.您需要使用包含" ip"或" mac"的字符串来指定参数,具体取决于您要检查的内容 ...
- linux qt获取网卡mac地址,QT实战获取主机名QT获取IP地址 获取MAC地址 获取广播地址...
QT的网络东西也蛮多的,我们今天先学一部分,然后后面在学一部分吧 一个能使用网络的应用,我们需要在项目工程中的pro文件里进行标记QT += core gui #标记 网络 QT += network ...
- python获取mac窗口坐标_[代码全屏查看]-Python3根据IP地址获取MAC地址
[1].[代码] [Python]代码 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import platform import ...
- JAVA获取局域网内对应IP电脑的MAC地址(物理地址)
创建类UdpGetClientMacAddr.java package com.shuzhiqiang.common;import java.io.IOException; import java.n ...
- java获取IP地址和MAC地址方式
java获取IP地址和MAC地址方式 前端和后台都可以获取 ip获取方式比较简单,就不做分析了,主要说mac的获取 前端获取的方式与局限性:尝试好几种方式,这种是最易实现.最简单的方式,确实能获取该浏 ...
- 获取IP地址和MAC地址
先获取IP地址,再通过IP地址获取MAC地址,Mark一下. //获得IP地址WSADATA wsaData;char name[155];char * ip;PHOSTENT hostinfo;if ...
最新文章
- Zabbix 3.0 部署监控 [三]
- 第三次学JAVA再学不好就吃翔(part51)--String类的转换功能
- 【Java】不正当使用break语句的危害
- Ruby1.9.3 Rails 3.2.3安装
- ios 开发需要看的书籍
- HTML+CSS+JS实现网页随机点名
- 年底跳槽,都去哪儿?数万从业者的新选择即将揭幕
- Qt基于FFmpeg读取摄像头并进行H264编码
- 常见距离算法-欧氏距离、杰卡德距离、余弦距离
- nsh 自定义启动脚本
- DataV(对象类)未来三天天气状态显示对应图标,格式:{ “results“:[ { “location“:{},“daily“:[ {},{}] ] }
- python实现模拟按键down_Python如何实现键盘鼠标按键模拟 Python实现键盘鼠标按键模拟代码...
- 【同步与补偿】频率偏移
- git 怎么切换分支命令_git命令-切换分支
- Blinker点灯科技绑定GitHub增加设备数量到10个
- 调试经验——键盘C键V键和H键失灵
- PL/SQL批量运行SQL语句
- Git “强制“提交代码
- Win11杜比全景声无法正常运行的解决方法教学
- NYOJ 118 次小生成树