如何在java中从路由器获取客户端列表

如何在java中从路由器获取客户端列表,java,Java,在java中,从路由器获取整个已连接客户端列表的最佳方法是什么 我认为应该这样做: 使用指定的根/密码配置属性对象,然后通过登录请求将其传递,最后获取客户端列表 提前感谢您的帮助。您可以为每个LAN ip运行一个循环,例如: 本地机器ip是192.168.1.2,您将从1循环到255,将最后一个数字更改为循环值,并检查是否可以访问。例如: private Collection<InetAddress> findLANMachines() throws IOException {

在java中,从路由器获取整个已连接客户端列表的最佳方法是什么

我认为应该这样做: 使用指定的根/密码配置属性对象,然后通过登录请求将其传递,最后获取客户端列表


提前感谢您的帮助。

您可以为每个LAN ip运行一个循环,例如: 本地机器ip是192.168.1.2,您将从1循环到255,将最后一个数字更改为循环值,并检查是否可以访问。例如:

private Collection<InetAddress> findLANMachines() throws IOException {
        String ip = "192.168.1."; //change that to whatever u want :- )
        ArrayList<InetAddress> lanMachines = new ArrayList<>();
        for (int i = 1; i <= 255; i++) {
            InetAddress a = InetAddress.getByName(ip+i);
            if(a.isReachable(2000)) //Change the timeout miliseconds to whatever u want, This may vary depending on the type of network you are on
                                    //and the capabilities of your card.
            lanMachines.add(a);
        }
        return lanMachines;
    }
private Collection findLANMachines()引发IOException{
String ip=“192.168.1.”;//将其更改为您想要的任何内容:-)
ArrayList=新的ArrayList();

对于(int i=1;我多一点信息将是有益的,对于路由器(HW/SW),它有一个基于Web的管理等等。@ NWDX,我不认为我们需要这个信息,特别是如果我们考虑SeaReo为公众提供一个你无法预测路由器类型和能力的地方。局域网扫描可以做这项工作,但是它需要……(任何路由器都有办法做到这一点吗?@user3121190哪种型号是你的路由器?它是硬件路由器还是软件路由器?因此,也许你的路由器已经提供了一个接口。它是一个硬件路由器编辑:Sitecom 300nHm,路由器是连接两个或多个网络的设备/软件,因此通过你的代码你无法看到连接的设备如果它们位于另一个网段,例如10.0.0.1,则在该路由器上。对所有专用网络进行全网络扫描可能会失败。如果路由器具有SNMP/脚本或任何其他接口,则这将是一个更可能的解决方案。这太完美了,谢谢!!很高兴它有帮助!但是@NwDx是对的,我已经将这种可能性炸开了:-(上述代码(第二件)将仅考虑其所属的网络范围(如192.168.X.X),但如果存在其他ip,则它将永远不会与它们联系:3如果您只有一个只有一个路由器且仅连接到internet的小型LAN,则fillpant解决方案将适用于您。作为一个例子,该解决方案在网络方面有其局限性lready提到过。但是如果简单的方法对您合适,我只能建议一点小小的改进——使用以下方式循环IP地址:新的子网地址(“192.168.1.0/24”).getInfo().getAllAddresses()。如果需要,您可以忽略网络和广播地址。
private Collection<InetAddress> findLANMachines() throws IOException {
        int depth = 1; //This number is the range from 1 to X representing * in this ip: 192.168.*.1
        int subDepth = 255; //This is the range from 1 to X representing * in this ip: 192.168.1.*
        int TIMEOUT = 1000;//The ammount to which we need to wait for the other host to reply before we skip to the next one (in ms)
        String ip = InetAddress.getLocalHost().toString().split("/")[1];
        String tmp = ip.substring(0,
                ip.lastIndexOf(".", ip.lastIndexOf(".") - 1))
                + ".";
        ArrayList<InetAddress> lanMachines = new ArrayList<>();
        for (int j = 1; j < depth; j++) {
            for (int i = 1; i < subDepth; i++) {
                InetAddress a = InetAddress.getByName(tmp + j + "." + i);
                if (a.isReachable(TIMEOUT))
                    lanMachines.add(a);
            }
        }
        return lanMachines;
    }