Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gettind a device MAC address by IP

Tags:

java

android

Well, in my app I use some code to discover all reachable devices in Local Network. Now I want to get device's mac address using local ip address

I want something like this: getMacAddress("192.168.1.23")

public static String getMacAddress(String ip)
{
    String macAddress = ...
    return macAddress;
}

I have found this for getting my own mac address, what about the rest of the ips in the LAN?

 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 WifiInfo info = manager.getConnectionInfo();
 String address = info.getMacAddress();
like image 567
JexSrs Avatar asked Nov 16 '25 07:11

JexSrs


2 Answers

/proc/net/arp is the local ARP cache, which at least has cached entries (which may be offline).

protected String getMacAddress(String ipAddress) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(new File("/proc/net/arp")));
        String line;
        while((line = br.readLine()) != null) {
            if(line.contains(ipAddress)) {
                /* this string still would need to be sanitized */
                return line;
            }
        }
        System.out.println(output);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

alternatively, one could scan the whole local network segment -

or retrieve the ARP cache from a local network router.

like image 197
Martin Zeitler Avatar answered Nov 17 '25 21:11

Martin Zeitler


Ref: How can I get the number of devices connected through the phones access-point?

you can count connected devices on access point and it's get hardware mac address on below link on android: http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/

code from above link:

/**
 * Try to extract a hardware MAC address from a given IP address using the
 * ARP cache (/proc/net/arp).<br>
 * <br>
 * We assume that the file has this structure:<br>
 * <br>
 * IP address       HW type     Flags       HW address            Mask     Device
 * 192.168.18.11    0x1         0x2         00:04:20:06:55:1a     *        eth0
 * 192.168.18.36    0x1         0x2         00:22:43:ab:2a:5b     *        eth0
 *
 * @param ip
 * @return the MAC from the ARP cache
 */

public static String getMacFromArpCache(String ip) {
    if (ip == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) {
                // Basic sanity check
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    return mac;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

or if u have problem on code try below code:

 public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
        ArrayList<InetAddress> ret = new ArrayList<InetAddress>();

        LoopCurrentIP = 0;

        String IPAddress = "";
        String[] myIPArray = YourPhoneIPAddress.split("\\.");
        InetAddress currentPingAddr;


        for (int i = 0; i <= 255; i++) {
            try {

                // build the next IP address
                currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
                        myIPArray[1] + "." +
                        myIPArray[2] + "." +
                        Integer.toString(LoopCurrentIP));
                ad = currentPingAddr.toString();   /////////////////
                Log.d("MyApp",ad);                 //////////////

                // 50ms Timeout for the "ping"
                if (currentPingAddr.isReachable(50)) {

                    ret.add(currentPingAddr);
                    ad = currentPingAddr.toString();        /////////////////
                    Log.d("MyApp",ad);                     //////////////
                }
            } catch (UnknownHostException ex) {
            } catch (IOException ex) {
            }

            LoopCurrentIP++;
        }
        return ret;
    }
like image 37
Ashvin solanki Avatar answered Nov 17 '25 20:11

Ashvin solanki