gpt4 book ai didi

java - 如何检测登录网络的设备?

转载 作者:行者123 更新时间:2023-12-01 12:13:22 25 4
gpt4 key购买 nike

我试图检测登录企业网络的任何设备(台式机、移动设备、平板电脑等)。这是我写的非常肮脏的代码。该代码不断检查已连接的设备,并通过检查一组来打印新设备。

public class DeviceDetectAgent {

private static Set<String> connectedDevicesPast = new HashSet<String>();

private static void detectNewDevices() {

private Set<String> connectedDevicesPresent = new HashSet<String>(); // saves all devices detected in the previous poll

InetAddress localhost = InetAddress.getLocalHost();
byte[] iPAddress = localhost.getAddress();

for (int i=1; i<=254; i++) {

iPAddress[3] = (byte) i;
InetAddress inetAddress = InetAddress.getByAddress(iPAddress);

if (inetAddress.isReachable(1000)) {

String device = inetAddress.toString();
if (!connectedDevicesPast.contains(device)) {
System.out.println("New device " + device + "found.");
}
connectedDevicesPresent.add(device);
}
}

connectedDevicesPast = connectedDevicesPresent;
}

public static void main(String[] args) {

while (true) {
detectNewDevices();
Thread.sleep(1000);
}
}
}

我的目标是创建一个代理来检测登录网络的设备。我可以对我的代码进行任何改进吗?我相信我的代码太简单了。

最佳答案

您的解决方案具有以下限制。

首先,设备可以使用其个人防火墙关闭 ICMP。在这种情况下,它不会对 ping 做出响应,您也不会看到它。

其次,你的解决方案非常慢。 Ping 可能需要一秒钟的时间,因此您将花费大约 4 分钟来完成循环。幸运的是这个问题可以使用 NIO 来解决。看看this code作为示例或查看异步 ping 的其他示例。

解决第一个问题并非易事。一般来说,你可以考虑两种策略:

  1. 尝试使用其他端口和协议(protocol)发现设备
  2. 尝试在设备执行某种网络 Activity 时捕获设备。

使用其他协议(protocol)有点复杂。看看 nmap - 已经可以做到这一点的工具。捕获其他网络 Activity 可以使用 PCAP 来完成,或者如果您在 java JPCAP 中实现此功能。然而,您应该将您的 spy 定位在网络中“正确”的位置。最好的方法是使用网络镜像。

关于java - 如何检测登录网络的设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com