Wi-Fi tethering - how to get list of connected clients

余生颓废 提交于 2019-11-29 21:29:25

问题


Is there any way to get a the list of connected MAC addresses when my phone is on Wi-Fi tethering mode?


回答1:


Firstly, you must have a rooted device. When it's done just read dnsmasq.leases file. Usually it is placed at: /data/misc/dhcp/dnsmasq.leases. A structure of the file is pretty simple - each line is a summary of a connected user. The summary has several fields including MAC. I didn't find a possibility to get MAC without root. Please correct me if I'm wrong.




回答2:


Reading /proc/net/arp would provide the information for both static and DHCP clients that have communicated with the device during the last 60 seconds (configured in /proc/sys/net/ipv4/neigh/wl0.1/gc_stale_time where wl0.1 is the wireless network interface on my phone).

It is available for non-root users too.




回答3:


@SuppressWarnings("ConstantConditions")
public static String getClientMacByIP(String ip)
{
    String res = "";
    if (ip == null)
        return res;

    String flushCmd = "sh ip -s -s neigh flush all";
    Runtime runtime = Runtime.getRuntime();
    try
    {
        runtime.exec(flushCmd,null,new File("/proc/net"));
    }

    BufferedReader br;
    try
    {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null)
        {
            String[] sp = line.split(" +");
            if (sp.length >= 4 && ip.equals(sp[0]))
            {Assistance.Log(sp[0]+sp[2]+sp[3],ALERT_STATES.ALERT_STATE_LOG);
                String mac = sp[3];
                if (mac.matches("..:..:..:..:..:..") && sp[2].equals("0x2"))
                {
                    res = mac;
                    break;
                }
            }
        }

        br.close();
    }
    catch (Exception e)
    {}

    return res;
}

//--------------------------------------------------------

@SuppressWarnings("ConstantConditions")
public static String getClientIPByMac(String mac)
{
    String res = "";
    if (mac == null)
        return res;

    String flushCmd = "sh ip -s -s neigh flush all";
    Runtime runtime = Runtime.getRuntime();
    try
    {
        runtime.exec(flushCmd,null,new File("/proc/net"));
    }

    BufferedReader br;
    try
    {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null)
        {
            String[] sp = line.split(" +");
            if (sp.length >= 4 && mac.equals(sp[3]))
            {
                String ip = sp[0];
                if (ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}") && sp[2].equals("0x2"))
                {
                    res = ip;
                    break;
                }
            }
        }

        br.close();
    }
    catch (Exception e)
    {}

    return res;
}



回答4:


public static ArrayList<String> getConnectedDevicesMac()
{
    ArrayList<String> res = new ArrayList<String>();
    //NetManager.updateArpFile();

    BufferedReader br;
    try
    {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        line = br.readLine();
        while ((line = br.readLine()) != null)
        {
            String[] sp = line.split(" +");
            if (sp[3].matches("..:..:..:..:..:.."))
                res.add(sp[3]);
        }

        br.close();
    }
    catch (Exception e)
    {}

    return res;
}


来源:https://stackoverflow.com/questions/9996351/wi-fi-tethering-how-to-get-list-of-connected-clients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!