如何使用java获取连接到LAN的所有系统名称?
我想让所有系统连接在一个 LAN 中? 有人可以建议我吗?
I want to get all the systems connected in a LAN?
Can anyone suggest me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想让所有系统连接在一个 LAN 中? 有人可以建议我吗?
I want to get all the systems connected in a LAN?
Can anyone suggest me?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
我在 Java 中使用的包名为 jcifs 来实现此目的。以下是库的链接。
请注意,要识别子网上的计算机,您需要 ping 子网上的所有可用 IP 地址。从那里您可以进行反向 IP 地址查找以获取机器详细信息。
根据 OSI 的记忆,您想要 ping 网络上的所有计算机的原因是因为 ICMP 仍然是 OSI 模型的最低层。尽管您不能信任仅使用 ICMP (ping) 请求来发送子网上的所有计算机。原因是大多数防止钓鱼攻击的 Windows 机器都会阻止该协议。因此,您需要采取两阶段检测方法。首先,使用 ICMP ping 请求通过子网 IP 地址进行广播。等待 1 秒后收到 ICMP ping 请求,继续执行与无响应 IP 地址的 SMB 连接。您会发现大多数人都会拥有 Microsoft 网络的共享打印机,该打印机将响应该端口下的请求。
另一种方法是使用 NetBios 反向名称查找,尽管它仍然涉及向子网的 IP 地址范围发送垃圾邮件。
希望这有帮助。
The Package I used in Java to achieve this was called jcifs. Here is the link to the Library .
Please note, to identify a machine on a subnet you will need to ping all the avaliable Ip addresses on the subnet. From there you can do a reverse IP Address lookup to get the machines details.
From memory from the OSI the reason why you want to ping all the machines on the network is because ICMP is still the lowest layer on the OSI Model. Though you just cannot trust just using ICMP (ping) requests to all machines on the subnet. The reason for this is most Windows Machines to prevent fishing attacks will block that protocol. So instead, you need to take a two stage detection approach. First, broadcast over the SubNet Ip address with a ICMP ping request. From the ICMP ping request after waiting 1 second, proceed to perform a SMB connection to the non responding IP addresses. You will find that most people will have either a shared printer of Microsoft Network that will respond to a request under that port.
The alternative is to use a NetBios reverse name lookup, though it still involves spamming the IP address range of the subnet.
Hope this helps.
如果我打算尝试在 Java 中实现此功能,我会选择一些未使用的 TCP/IP 端口号,然后尝试为 LAN IP 地址范围中的每个 IP 地址打开一个套接字。您预计所有连接尝试都会失败,但它们应该以不同的方式失败,具体取决于计算机是否正在使用该 IP 地址。如果 IP 地址正在使用中,您应该会收到“连接被拒绝”的消息。如果未使用,您应该得到“没有到主机的路由”或“没有到网络的路由”。 “连接超时”可能表明该主机被防火墙屏蔽,或者该主机最近处于活动状态但此时不活动。
另一种方法(我不知道您是否可以通过 Java 实现)是为网络地址范围内的每个 IP 地址发送 ARP 请求,然后查看计算机的 ARP 缓存中出现的内容。
当然,您可以尝试发送 ICMP Ping 消息,但它们可能受到防火墙的限制。
获得实时 IP 地址列表后,使用 DNS 反向查找来查找相应的 DNS 名称。但请注意,并非所有 IP 地址都绑定到 DNS 名称。
然而,考虑到许多机器和网络使用各种防火墙,并且这些防火墙通常会丢弃网络消息或发送误导性响应,因此这一切都有点脆弱。
If I was going to try to implement this in Java, I'd pick some unused TCP/IP port number and then try to open a socket to each IP address in the LAN's IP address range(s). You expect all the connection attempts to fail, but they should fail in different ways depending on the whether a machine is using the IP address. If an IP address is in use, you should get a "connection refused". If it is not in use, you should get a "no route to host" or "no route to network". A "connect timed out" may indicate that the host is fire-walled, or that was recently alive but is not alive at the moment.
Another approach (which I don't know is you can do from Java) is to send ARP requests for each of the IP addresses in the network address range and see what turns up in your machine's ARP cache.
And of course, you can try sending ICMP Ping messages, but they may be firewalled.
Once you have a list of live IP addresses, use DNS reverse lookup to find the corresponding DNS names. But beware that not all IP addresses are bound to DNS names.
However this is all a bit tenuous given that a lot of machines and networks use firewalls of various kinds, and these are wont to drop network messages or send misleading responses.
为什么你想要它们,为什么它们必须是名字?并非所有系统都有名称。
您想列举哪些类型的系统?
如果您尝试定位您自己的应用程序的附近实例,请使用多播 UDP 来创建发现协议。
如果您试图找到某个有自己的协议用于此目的的服务器,请使用它的协议。
Why do you want them, and why must they be names? Not all systems have a name.
What kind of systems are you trying to enumerate?
If you're trying to locate nearby instances of your own application, use multicast UDP to create a discovery protocol.
If you're trying to locate some server which has its own protocol for that purpose, then use its one.