如何使用Java代码获取访问点的MAC地址? [英] How do I get MAC address of a access point using java code?

查看:46
本文介绍了如何使用Java代码获取访问点的MAC地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java初学者,我想帮助解决我的问题.我想使用Java语言检测访问点并显示访问点的mac地址.我有3个类别,只能检测主机的ip地址和mac地址.以下是我的代码.

 head.java
公共 
{
    公共 静态  void  main(字符串 [] args)抛出 InterruptedException
    {
        字符串 tracerserver = " ; 跟踪服务器地址
         int  timeRange = 1; // 以分钟为单位

        System.out.println(" );

         while (真)
        {
            尝试
            {
                tracer machine =  tracer();
                //  System.out.println("IP地址:" + machine.ipaddr()); 
                //  System.out.println("Mac地址:" + machine.macAddr()); 

                字符串 requestParameters = "  + machine.ipaddr()+ "  + machine.macAddr()+ " ;
                sender.sendGetRequest(tracerserver,requestParameters);

                Thread.sleep((timeRange *  60000 ));
            }
            捕获(可抛出t)
            {
                System.err.println("  + t);
            }
        }
    }
}

sender.java
导入 java.io.BufferedReader;
导入 java.io.InputStreamReader;
导入 java.net.*;
公共 发​​件人
{
    公共 sender(){}

    公共 静态 字符串 sendGetRequest(字符串终结点,字符串 requestParameters)
    {
        字符串 result = null;
        如果(endpoint.startsWith(" ))
        {
            // 向Servlet发送GET请求
            尝试
            {
                字符串 urlStr =端点;
                如果(requestParameters!= null&&requestParameters.length()>  0 )
                {
                    urlStr + = "  + requestParameters;
                }
                URL url =  URL(urlStr);
                URLConnection conn = url.openConnection();
                // 获取响应
                BufferedReader rd =  BufferedReader( InputStreamReader(conn.getInputStream()));
                StringBuffer sb =  StringBuffer();
                字符串行;
                 while ((line = rd.readLine())!= null)
                {
                    sb.append(line);
                }
                rd.close();
                结果= sb.toString();
            }
            捕获(异常e)
            {
                e.printStackTrace();
            }
        }
        返回结果;
    }
}

tracer.java
导入 java.net.InetAddress;
导入 java.net.NetworkInterface;
导入 java.net.SocketException;
导入 java.net.UnknownHostException;
导入 com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
公共 跟踪器
{
    公共 tracer(){}
    公共 字符串 ipaddr()
    {
        尝试
        {
            InetAddress localaddr = InetAddress.getLocalHost();
            返回 localaddr.getHostAddress();
        }
        捕获(UnknownHostException e)
        {
            返回 "  + e;
        }
    }

    公共 字符串 macAddr()
    {
        尝试
        {
            InetAddress地址= InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            如果(ni!= null)
            {
                字节 [] mac = ni.getHardwareAddress();
                如果(mac!= null)
                {
                    字符串 aa = HexBin.encode(mac);
                    字符串 ab = " ;
                     int  a = 0;
                     int  b = 2;

                     for ( int  i =  0 ; i < mac.length; i ++)
                    {
                        ab + = aa.substring(a,b)+((i< mac.length- 1 )?" span> -": ") ;
                        a = a + 2;
                        b = b + 2;
                    }
                    返回 ab;
                }
                其他
                {
                    返回 " ;
                }
            }
            其他
            {
                找不到返回 " ;
            }
        }
        捕获(UnknownHostException e)
        {
            e.printStackTrace();
        }
        捕获(SocketException e)
        {
            e.printStackTrace();
        }
        返回 null;
    }
} 



希望有人能帮助我解决这个问题.

解决方案

我想您可以进行tracert并从列表中报告的第一个跃点中检索IP地址.

先生,如果您已完成操作的话.请将代码发送给我,我的电子邮件ID.我已经尝试过,但是做不到.
我的电子邮件ID为<   b  >  sana.asghar928@gmail.com <  /b   > 

萨娜·阿斯加(Sana Asghar)
谢谢.


Hi, I''m a java beginner and I would like help to solve my problem. I want to use java language to detect an access point and display the mac address of the access point. I have 3 classes and can only detect ip address and mac address of a host. Below is my code.

head.java
public class head
{
    public static void main(String[] args) throws InterruptedException
    {
        String tracerserver="http://127.0.0.1/eds/track.php";//tracker server address
        int timeRange=1;//in minutes

        System.out.println("Application running now...!");

        while(true)
        {
            try
            {
                tracer machine = new tracer();
                //System.out.println("Ip address: "+machine.ipaddr());
                //System.out.println("Mac Address: "+machine.macAddr());

                String requestParameters ="ip="+machine.ipaddr()+"&mac="+machine.macAddr()+"&point=3.223,2.93";
                sender.sendGetRequest(tracerserver, requestParameters);

                Thread.sleep((timeRange*60000));
            }
            catch(Throwable t)
            {
                System.err.println("Error:"+t);
            }
        }
    }
}

sender.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class sender
{
    public sender(){}

    public static String sendGetRequest(String endpoint, String requestParameters)
    {
        String result = null;
        if (endpoint.startsWith("http://"))
        {
            //Send a GET request to the servlet
            try
            {
                String urlStr = endpoint;
                if (requestParameters != null && requestParameters.length () > 0)
                {
                    urlStr += "?" + requestParameters;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection ();
                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null)
                {
                    sb.append(line);
                }
                rd.close();
                result = sb.toString();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
}

tracer.java
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
public class tracer
{
    public tracer(){}
    public String ipaddr()
    {
        try
        {
            InetAddress localaddr = InetAddress.getLocalHost();
            return  localaddr.getHostAddress();
        }
        catch(UnknownHostException e)
        {
            return "Can't detect localhost : " + e;
        }
    }

    public String macAddr()
    {
        try
        {
            InetAddress address = InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            if (ni != null)
            {
                byte[] mac = ni.getHardwareAddress();
                if (mac != null)
                {
                    String aa = HexBin.encode(mac) ;
                    String ab = "";
                    int a=0;
                    int b=2;

                    for (int i = 0; i < mac.length; i++)
                    {
                        ab+=aa.substring(a, b)+ ((i < mac.length - 1) ? "-" : "") ;
                        a=a+2;
                        b=b+2;
                    }
                    return ab;
                }
                else
                {
                    return "Address doesn't exist or is not accessible.";
                }
            }
            else
            {
                return "Network Interface for the specified address is not found.";
            }
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}



Hope that somebody help me in this problem.

解决方案

I suppose you could do a tracert and retrieve the IP address from the first reported hop in the list.


Sir if you have done with it. kindly send me a code to my email id. i have tried but couldnt able to do it.
my email id is <b>sana.asghar928@gmail.com</b>

Sana Asghar
thanks.


这篇关于如何使用Java代码获取访问点的MAC地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆