首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >点击整个网络查找设备

点击整个网络查找设备
EN

Stack Overflow用户
提问于 2017-02-20 19:08:55
回答 1查看 105关注 0票数 1

我试图在5000端口上从192.168.1.0192.168.1.255的本地网络进行ping,而Arduino板也用5000端口连接到网络。我有主板的Mac地址,并试图找到IP地址。这是我的密码

代码语言:javascript
复制
static void pingLocal() {
    for (int i = 0; i <= 255; i++) {
        ping("192.168.1." + i + ":5000");
    }
}

private static void ping(String url) {
    try {
        Process mIpAddrProcess = Runtime.getRuntime().exec("/system/bin/ping -c 1 " + url);
        int mExitValue = mIpAddrProcess.waitFor();
        System.out.println(" mExitValue " + mExitValue);
        if (mExitValue == 0) {
            Log.d("log", "true");
        } else {
            Log.d("log", "false");
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

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

这就是结果

代码语言:javascript
复制
02-20 19:57:07.465 12103-12103/ir.shafadoc.handset D/line: IP address       HW type     Flags       HW address            Mask     Device
02-20 19:57:07.466 12103-12103/ir.shafadoc.handset D/line: 192.168.1.33     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.466 12103-12103/ir.shafadoc.handset D/line: 192.168.1.26     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.467 12103-12103/ir.shafadoc.handset D/line: 192.168.1.19     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.467 12103-12103/ir.shafadoc.handset D/line: 192.168.1.12     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.467 12103-12103/ir.shafadoc.handset D/line: 192.168.1.31     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.468 12103-12103/ir.shafadoc.handset D/line: 192.168.1.24     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.468 12103-12103/ir.shafadoc.handset D/line: 192.168.1.17     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.469 12103-12103/ir.shafadoc.handset D/line: 192.168.1.10     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.470 12103-12103/ir.shafadoc.handset D/line: 192.168.1.29     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.470 12103-12103/ir.shafadoc.handset D/line: 192.168.1.22     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.470 12103-12103/ir.shafadoc.handset D/line: 192.168.1.15     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.471 12103-12103/ir.shafadoc.handset D/line: 192.168.1.8      0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.471 12103-12103/ir.shafadoc.handset D/line: 192.168.1.1      0x1         0x2         c0:a0:bb:9a:e4:ad     *        wlan0
02-20 19:57:07.472 12103-12103/ir.shafadoc.handset D/line: 192.168.1.27     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.472 12103-12103/ir.shafadoc.handset D/line: 192.168.1.20     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.472 12103-12103/ir.shafadoc.handset D/line: 192.168.1.13     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.473 12103-12103/ir.shafadoc.handset D/line: 192.168.1.32     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.473 12103-12103/ir.shafadoc.handset D/line: 192.168.1.25     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.473 12103-12103/ir.shafadoc.handset D/line: 192.168.1.18     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.474 12103-12103/ir.shafadoc.handset D/line: 192.168.1.11     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.474 12103-12103/ir.shafadoc.handset D/line: 192.168.1.30     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.475 12103-12103/ir.shafadoc.handset D/line: 192.168.1.23     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.475 12103-12103/ir.shafadoc.handset D/line: 192.168.1.16     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.475 12103-12103/ir.shafadoc.handset D/line: 192.168.1.9      0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.476 12103-12103/ir.shafadoc.handset D/line: 192.168.1.28     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.476 12103-12103/ir.shafadoc.handset D/line: 192.168.1.21     0x1         0x0         00:00:00:00:00:00     *        wlan0
02-20 19:57:07.477 12103-12103/ir.shafadoc.handset D/line: 192.168.1.14     0x1         0x0         00:00:00:00:00:00     *        wlan0

如您所见,arp缓存中只有调制解调器具有Mac地址。怎么啦?如何发现网络并从Mac中查找IP地址?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-20 20:53:54

我发现这种方法可以平平网络,而且效果很好

代码语言:javascript
复制
if (InetAddress.getByName(host).isReachable(timeout)) {
      System.out.println(host + " is reachable");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42352482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档