简体   繁体   English

在局域网中获取所有Up IP -java

[英]Get all the Up ip in the local network -java

For the Java project i need to scan the list of ip connected to the same local network via wlan or eth0 or anything. 对于Java项目,我需要扫描通过wlan或eth0或其他任何方式连接到同一本地网络的ip列表。 I need to get the list of ip address that are up in the local network . 我需要获取本地网络中的IP地址列表

I tried 我试过了

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) 
{
        System.out.println(address.getNetworkPrefixLength());
}

But it gives 但是它给

Exception in thread "main" java.lang.NullPointerException
at com.Server.Subnet.main(Subnet.java:17)

I think i need to follow these steps. 我认为我需要遵循以下步骤。

  1. Get the subnet address of the network that i connected 获取我连接的网络的子网地址
  2. Scan all the ip address in the subnet mask 扫描子网掩码中的所有IP地址
  3. List the ip address that are up 列出已启用的IP地址

Can you give me the right implementation way 你能给我正确的实现方式吗

Follow these directions 遵循这些指示
-- get your system IP -获取系统IP
-- get your subnet mask. -获取您的子网掩码。
-- As per your subnet mask, get the list of possible IP addresses in your subnet.& -根据您的子网掩码,获取子网中可能的IP地址的列表。
-- Now, one by one ping them. -现在,一一ping通它们。 (you can use system ping command with java) (您可以在Java中使用system ping命令)
-- check ping response, then you can decide whether the host is up or not. -检查ping响应,然后可以确定主机是否启动。

I tried this program to find all the up ip in the subnet of the system connected. 我尝试使用此程序在连接的系统的子网中查找所有up ip。

package com.Server;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class Subnet
{
    public void Subnet() throws UnknownHostException, SocketException
    {
        Enumeration e = NetworkInterface.getNetworkInterfaces();
        while(e.hasMoreElements())
        {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration ee = n.getInetAddresses();
            while (ee.hasMoreElements())
            {
                InetAddress i = (InetAddress) ee.nextElement();
                String ip = i.getHostAddress();

                String sip = ip.substring(0, ip.indexOf('.',ip.indexOf('.',ip.indexOf('.')+1) + 1) + 1);
                try {
                    for(int it=1;it<=255;it++)
                    {
                        String ipToTest = sip+it;
                        boolean online = InetAddress.getByName(itToTest).isReachable(100);
                        if (online) {
                            System.out.println(ipToTest+" is online");
                        }

                    }
                } catch (IOException e1) {
                    System.out.println(sip);
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM