对于Java项目,我需要扫描通过wlan或eth0或任何东西连接到同一个本地网络的ip列表。我需要获得本地网络上ip地址的列表。
我试过了
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
for (InterfaceAddress address : networkInterface.getInterfaceAddresses())
{
System.out.println(address.getNetworkPrefixLength());
}但它给了
Exception in thread "main" java.lang.NullPointerException
at com.Server.Subnet.main(Subnet.java:17)我想我需要遵循这些步骤。
你能给我正确的实现方式吗?
发布于 2014-12-28 04:59:51
我试着用这个程序来查找系统子网中所有的up。
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);
}
}
}
}
}发布于 2014-12-26 06:27:24
按照以下指示
得到你的系统IP
把你的子网面具拿来。
根据子网掩码,获取子网中可能的IP地址列表。&
快.现在,一个接一个地打压他们。(您可以在java中使用system命令)
检查ping响应,然后决定主机是否启动。
https://stackoverflow.com/questions/27652171
复制相似问题