java获取来访者mac信息
根据IP获取对应的Mac地址,支持win10+Linux
package com.simonjia.util.other; /**
* @Author: SimonHu
* @Date: 2019/6/13 11:03
* @Description:
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MacAddress { /**
* 调用命令
* @param cmd
* @return
*/
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);
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, String sourceString, final String macSeparator) {
String result = "";
int index = sourceString.indexOf(ip);
if (index == -1) {
index = 0;
}
sourceString = sourceString.substring(index, sourceString.length() - 1);
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;
} /**
* 获取MAC地址
* @return 返回MAC地址
*/
public static String getMacAddress(String ip) {
String macAddress = "";
macAddress = getMacInWindows(ip).trim();
if (macAddress == null || "".equals(macAddress)) {
macAddress = getMacInLinux(ip).trim();
}
return macAddress;
} // //做个测试
public static void main(String[] args) {
System.out.println(MacAddress.getMacAddress("222.129.19.10"));
}
}
获取同一局域网内的所有IP和对应的Mac
package com.simonjia.util.other; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 获取同一局域网内的所有IP和对应的Mac
* @author liuyazhuang
*
*/
public class AllAddress { /**
* 获取统一局域网的所有IP地址
* @return 所有IP地址的List集合
*/
public static List<String> getIPs() {
List<String> list = new ArrayList<String>();
Runtime r = Runtime.getRuntime();
Process p;
try {
p = r.exec("arp -a");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inline;
while ((inline = br.readLine()) != null) {
if(!"".equals(inline.trim())){
if (inline.indexOf("---") > -1) {
continue;
}
if(inline.indexOf("Internet") > -1){
continue;
}
// 有效IP
String[] str = inline.split(" {4}");
list.add(str[0]);
// System.out.println(inline);
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
} /**
* 获取同一局域网内的IP和Mac
* @return 以IP地址为Key, Mac地址为Value的Map
*/
public static Map<String, String> getAllIPAndMac(){
Map<String,String> map = new HashMap<String,String>();
Runtime r = Runtime.getRuntime();
Process p;
try {
p = r.exec("arp -a");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inline;
while ((inline = br.readLine()) != null) {
if(!"".equals(inline.trim())){
if (inline.indexOf("---") > -1) {
continue;
}
if(inline.indexOf("Internet") > -1){
continue;
}
// 有效IP
String[] arr = inline.split(" {4}");
String ip = arr[0].trim();
String mac = "00-00-00-00-00-00";
for(int i = 1; i < arr.length; i ++){
String str = arr[i].trim();
if(stringIsMac(str)){
mac = str;
break;
}
}
map.put(ip, mac);
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return map;
} /**
* 根据正则表达式判断是否为Mac地址
* @param val
* @return true:是Mac地址,false:不是Mac地址
*/
private static boolean stringIsMac(String val) {
String trueMacAddress = "^([0-9a-fA-F]{2})(([/\\s:-][0-9a-fA-F]{2}){5})$";
// 这是真正的MAC地址;正则表达式;
return val.matches(trueMacAddress);
} /**
* 根据IP提取主机名
* @param ips
* @return 以IP地址为Key,主机名为Value的Map
*/
public static Map<String, String> getHostnames(List<String> ips){
Map<String,String> map = new HashMap<String,String>();
System.out.println("正在提取hostname...");
for(String ip : ips){
String command = "ping -a " + ip;
Runtime r = Runtime.getRuntime();
Process p;
try {
p = r.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream()));
String inline;
while ((inline = br.readLine()) != null) {
if(inline.indexOf("[") > -1){
int start = inline.indexOf("Ping ");
int end = inline.indexOf("[");
String hostname = inline.substring(start+"Ping ".length(),end-1);
System.out.println(hostname);
map.put(ip,hostname);
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("提取结束!");
return map;
} public static void main(String[] args) {
System.out.println(getIPs());
System.out.println(getAllIPAndMac());
}
}
参考:https://blog.csdn.net/l1028386804/article/details/46049885
java获取来访者mac信息的更多相关文章
- java获取当前操作系统的信息
java获取当前操作系统的信息 JavaOS虚拟机UnixEXT 从网上收集的一些关于java获取操作系统信息的方法,现在总结一下: 1获取本机的IP地址: private static Strin ...
- java获取天气预报的信息
运行效果: 主要功能: 1,jsp页面输入省份和城市 根据条件获取当地的天气信息 2,java代码 利用第三方的省份和城市的路径地址 本工程主要实现java获取天气预报的信息步骤1,创建工程weath ...
- java 获取本地 mac 地址
主要参考:Java获取本机MAC地址/IP地址/主机名 做的更改: 1.我的windows是中文版,程序中获取mac时是按照physical address 获取的,添加上"物理地址&quo ...
- Java 获取到配置文件信息
Java程序将数据库或者服务器IP写入到代码中,难免缺少灵活性. 如果写入到配置文件,部署到不通服务器上,只需要修改配置文 件即可. Java怎么读取配置文件 /** * 获取到配置文件信息 * @p ...
- Java 获取所有子类信息
我以前的博客(Java Scala获取注解的类信息)介绍过通过Reflections工具通过使用特定注解的类的信息,其实本工具也可以获取接口,抽象类,类等的所有子类信息.使用方法如下: Reflect ...
- Java获取电脑硬件信息
package com.szht.gpy.util; import java.applet.Applet; import java.awt.Graphics; import java.io.Buffe ...
- java获取类的信息
关键技术剖析 1.java.lang.reflect包实现了java的反射机制,在使用反射机制时,需要导入该包. 2.Class类的forName方法能够根据类名加载类,获得类的Class对象. Cl ...
- java获取操作系统的MAC地址和硬盘序列号
1.判断操作系统是Windows还是Linux private static Boolean isLinux() { String os = System.getProperty("os.n ...
- java获取电脑mac物理地址
import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import ...
随机推荐
- PHP获取某段文字作为标题
<?php mb_internal_encoding('utf-8'); // 提取文字标题,多余文字用省略号替换 $arr=[ '用心用情用功,进行无愧于时代的文艺创造', '一图了解第二届一 ...
- macbook打印出现乱码解决方案
系统偏好设置 --> 打印机与扫描仪 --> + (左下角的加号) --> IP --> 输入打印机的ip地址,然后最下面的 “使用选择” 中选中 普通PCL 打印机,(默认的 ...
- Redis-String常用命令
Redis-String常用命令 set key value- 设置Key-value键值对 get key 获取指定key对应的值 append key value 在指定key对应值的后面追加va ...
- 2.vi 和 vim 编辑器
Linux系统的命令行下的文本编辑器 三种模式 一般模式:打开文档的默认模式 编辑模式 可以进行编辑,要按下 i a o r 等字母后才能从一般模式进入编辑模式 按下ESC 退出编辑模式 命令 ...
- Delphi 建立ODBC数据源
樊伟胜
- 记一次自启动的docker容器将宿主机的开机用户登录界面覆盖事件
宿主机的系统为CentOS7_7.7.1908,默认为GUI启动,安装了宝塔面板,docker-ce为最新版. 在启动了一个centos7的容器(镜像为centos官方镜像)后,将该容器重启策略设置为 ...
- 【C】文件操作
文件操作 文件的打开 FILE * fopen(const char filename,const char * mode); 文件的打开操作 fopen 打开一个文件 (几种操作文件的组合) 文件的 ...
- Redis02——Redis单节点安装
Redis单节点安装 一.Redis的数据类型 string hash list set zset 二.安装 2.1.下载 wget http://download.redis.io/releases ...
- html标签被div嵌套页面字体变大的解决办法
html标签被div嵌套页面字体变大的解决办法 <div> <html> <head> <title></title> </head& ...
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期
Interrupt学习: 在jdk中关于interrupt相关方法有三个,如下: 关于上面的疑问会在稍后进行阐述滴,下面看代码: 编译运行: 应该说是t线程为啥在被打断之后没有退出,还是在运行状态,这 ...