一、环境介绍
1.最近在使用struts2和JSP开发web网页时,碰到一个问题,我需要在后台根据ip获取访问服务器的物理地址,然后进行其他业务处理,但是我发现我在后台没有对应的接口或函数能够满足我。当时有想过使用active控件方式获取,但是这样局限性太大,又要麻烦客户安装控件,于是就寻找纯Java代码获取客户端的MAC物理地址,在网上找了很久,终于找到一个代码,现在分享出来给大家,希望有用得到的朋友拿去使用做参考。
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- *
- * 根据ip获取mac地址
- *
- */
- public class GetMacAddress {
- 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, final String sourceString,final String macSeparator) {
- String result = "";
- 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(getMacAddress("192.168.2.184"));
- }
-
- }
复制代码
2.下面我说说代码整体思路,代码主要通过windows系统或Linux系统的命令来根据ip获取MAC物理地址,首先使用ping命令,如果能正常ping通,系统会缓存访问过的ip和MAC地址,然后通过arp命令去获取所有访问过的缓存ip和MAC地址信息,然后通过java字符串处理代码进行处理即可获取想要的IP地址对应的MAC地址。下面是具体代码(如果服务端是Linux系统建议在使用ARP命令时最好一条命令能过滤出IP对应的MAC地址,下次我将补上这一条命令):
二、源码下载
1.下载地址: GetMacAddress.java (4.19 KB, 下载次数: 6)
|
A beautiful web dashboard for Linux
最近提交(Master分支:3 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
所有评论(0)