如何获取在特定端口上运行的子网的所有 IP 地址

发布于 2024-12-28 16:09:35 字数 697 浏览 2 评论 0原文

我想找到位于同一本地网络上的所有系统,即具有相同子网掩码的系统。

我想我必须使用 Java 中的 InetAddress 类,但我不知道应该做什么。 我的算法是:

  1. 查找本地网络中所有可用的系统

  2. 检查它们是否在我请求的端口上运行

  3. 记住在此端口上运行的系统的 IP

InetAddress localHost = Inet4Address.getLocalHost(); 
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
System.out.println(networkInterface);
networkInterface.getByInetAddress(localHost);
    
for (){
    networkInterface.getByInetAddress(localHost) 
}

我认为 networkInterface.getByInetAddress(localHost) 可以为我提供子网中的所有可用 IP 地址,但我不知道如何做到这一点对于所有可用系统,以及 for 子句的条件是什么。

I want to find all systems that are on the same local network i.e. that have the same subnet mask.

I think I must work with InetAddress Class in Java but I don't know what should I do.
My algorithm is:

  1. Find all available system in a local network

  2. Check if they run on a my requested port

  3. Keep in mind the IP of system which runs on this port

InetAddress localHost = Inet4Address.getLocalHost(); 
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
System.out.println(networkInterface);
networkInterface.getByInetAddress(localHost);
    
for (){
    networkInterface.getByInetAddress(localHost) 
}

I think that the networkInterface.getByInetAddress(localHost) can give me all available IP addresses in a subnet, but I don't how I can do this for all available systems, and what is the condition for the for clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2025-01-04 16:09:35

查找 IP 子网中所有可能 IP 地址非常简单。只需编写一个循环,枚举子网中所有可能的 byte[] 表示形式,并为它们构造 InetAddress 实例。

这是简单的部分,困难的部分是解决这两个问题:

  • 如何找到子网上所有活动 IP 地址,以及

  • 对于给定的 IP 地址,您如何确定它是否正在使用特定端口。

第一个问题可以通过两种方式解决:

  • 您可以使用 InetAddress.isAlive() 方法来查看主机是否响应 ping。问题是某些主机可能被配置为不响应 ping,或者 ping 可能被内部防火墙阻止。如果子网很大,这也不起作用。问题是您需要 ping 大量主机,这将花费很长时间并产生大量网络流量...特别是如果有多个应用程序实例执行此操作。

  • 更明智的选择是检查本地计算机的 ARP 缓存,并提取它知道的所有 IP 地址。问题是1)你的应用程序可能没有访问ARP缓存的访问权限,2)某些IP地址可能不在缓存中,3)纯Java中没有办法访问ARP缓存。

第二个问题取决于端口上服务的性质:

  • 如果它是基于 TCP 的服务,那么您可以尝试使用普通套接字连接到 IP/端口。如果连接被接受,则表明该端口正在被某些东西使用。它可能是也可能不是您期望的服务,但一般来说,判断这一点的唯一方法是尝试使用该服务。

  • 如果它是基于 UDP 的服务,那么通常有现在的方法来知道是否有东西正在使用该端口。

Finding all of the possible IP addresses in an IP subnet is simple. Just write a loop that enumerates all possible byte[] representations in the subnet and constructs InetAddress instances for them.

That's the easy bit, the hard bit is solving these two problems:

  • how do you find all if the live IP addresses on a subnet, and

  • for a given IP address, how do you figure out if it is using a particular port.

The first problem can be solved in two ways:

  • You could use the InetAddress.isAlive() method to see if the host responds to a ping. The problem is that some hosts may be configured to not respond to pings, or the pings may be blocked by an internal firewall. This also doesn't work if the subnet is large. The problem is that you need to ping a large number of hosts, and that will take a long time and generate a lot of network traffic ... especially if there are multiple application instances doing this.

  • The smarter alternative is to examine your local machine's ARP cache, and extract all IP addresses that it knows about. The problem is that 1) your application may not have the access rights to access the ARP cache, 2) some IP addresses may not be in the cache, and 3) there is no way to access the ARP cache in pure Java.

The second problem depends on the nature of the service on the port:

  • If it is a TCP-based service, then you can attempt to connect to an IP/port using a plain socket. If the connection is accepted, then the port is in use by something. It may or may not be the service you expect, but in general the only way to tell that is to try to use the service.

  • If it is a UDP-based service, then there's now way in general to know if something is using the port.

宛菡 2025-01-04 16:09:35

我希望您的意思如下:

“我希望发现与(运行我的程序的计算机)的主 IP 地址位于同一本地网络上的所有系统,这些系统正在侦听某个 TCP 端口。”

对于 UDP,这是一个稍微困难的问题 - 您可以寻找反弹,但您的数据包可能被吃掉或丢失(回复也可能如此)。

现在,假设使用 TCP,您需要执行三个步骤。

  1. 获取您认为是“主要”接口的本地接口的 IP 地址。
  2. 枚举与该地址位于同一子网上的主机。
  3. 对于每个主机,确定适当的端口是否正在侦听。

对于步骤 1,请使用 InetAddress.getLocalHost()

对于步骤 2,获取检索到的地址的子网部分,然后尝试该子网内的每个可能的主机。我们希望它是 C 级主机,并且只有 254 个主机。 没有 100% 确定的方法可以在不尝试每一个主机的情况下获得所有主机。一种非 100% 确定(且更复杂)的方法是使用 ARP 表获取路由器的网络视图,向本地网络发送广播 ping,或者下降到第 2 层并发送广播以太网数据包。

对于步骤 3,在相应端口上打开与每个主机的连接,然后将其关闭。如果连接超时或被拒绝,请刮擦主机。否则,将其添加到“收听”列表中。您可能想一次尝试多个连接,并将超时间隔设置为较小的值,因为大多数人不会倾听。

第四步:恭喜!您刚刚用 Java 制作了一个功能非常匮乏的 nmap 版本。

I'm going to hope that you mean the following:

"I wish to discover all systems on the same local network as (the computer running my program)'s primary IP address which are listening to a certain TCP port."

With UDP, this is a slightly harder problem - you could look for bounces, but your packet may be eaten or lost (as could the reply).

Now, assuming TCP, you have three steps.

  1. Get the IP address of the local interface you believe is the "primary" one.
  2. Enumerate hosts on the same subnet as this address.
  3. For each host, determine if the appropriate port is listening.

For step 1, use InetAddress.getLocalHost().

For step 2, get the subnet portion of the retrieved address, and then try every possible host within that subnet. Let's hope it's a Class C and there are only 254 hosts. There is no 100% sure way to get all the hosts without trying each one. A non-100%-certain (and more complex) way is to use ARP tables to get a router's view of the network, to send a broadcast ping to the local net, or to fall down to layer 2 and send a broadcast Ethernet packet.

For step 3, open a connection to each host on the appropriate port and then close it. If the connection times out or is denied, scratch the host. Otherwise, add it to the "listening" list. You probably want to try more than one connection at a time and set the timeout interval to something small, since most people won't be listening.

Step 4: congratulations! You've just made a very feature-poor version of nmap in Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文