如何用java获取本地系统的子网掩码?

9fkzdhlc  于 2023-03-28  发布在  Java
关注(0)|答案(9)|浏览(136)

如何使用Java获取本地系统的Subnet掩码地址?

omjgkv6w

omjgkv6w1#

localhost接口的第一个地址的网络掩码:

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();

更全面的方法:

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

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

/24表示255.255.255。

x6yk4ghg

x6yk4ghg2#

我发现:

NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

要获取ipv6的子网掩码,我们可以用途:

networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();

要获取ipv4的子网掩码,我们可以用途:

networkInterface.getInterfaceAddresses().get(1).getNetworkPrefixLength();
8fsztsew

8fsztsew3#

您可以将ipv4获取的值转换为标准的文本ipv4格式,如下所示:

short prflen=...getNetworkPrefixLength();
int shft = 0xffffffff<<(32-prflen);
int oct1 = ((byte) ((shft&0xff000000)>>24)) & 0xff;
int oct2 = ((byte) ((shft&0x00ff0000)>>16)) & 0xff;
int oct3 = ((byte) ((shft&0x0000ff00)>>8)) & 0xff;
int oct4 = ((byte) (shft&0x000000ff)) & 0xff;
String submask = oct1+"."+oct2+"."+oct3+"."+oct4;
bbuxkriu

bbuxkriu4#

SE6中的java.net.InterfaceAddress有一个getNetworkPrefixLength方法,顾名思义,该方法返回网络前缀长度。如果您希望使用该格式,则可以从中计算子网掩码。java.net.InterfaceAddress支持IPv4和IPv6。
多个网络应用程序API中的getSubnetMask()以java.net.InetAddress的形式返回指定IP地址的子网掩码(一个本地系统可能有多个本地IP地址)

zvms9eto

zvms9eto5#

我设计了一个足够简单的IPv4解决方案。我需要它来为这里的子网生成子网掩码,以便正确地委派这些子网。我知道我可以生成一个包含32个可能掩码的表,但我更喜欢每次都计算它。
这是我的解决方案。

/*
 * Get network mask for the IP address and network prefix specified...
 * The network mask will be returned has an IP, thus you can
 * print it out with .getHostAddress()...
 */
public static InetAddress getIPv4LocalNetMask(InetAddress ip, int netPrefix) {

    try {
        // Since this is for IPv4, it's 32 bits, so set the sign value of
        // the int to "negative"...
        int shiftby = (1<<31);
        // For the number of bits of the prefix -1 (we already set the sign bit)
        for (int i=netPrefix-1; i>0; i--) {
            // Shift the sign right... Java makes the sign bit sticky on a shift...
            // So no need to "set it back up"...
            shiftby = (shiftby >> 1);
        }
        // Transform the resulting value in xxx.xxx.xxx.xxx format, like if
        /// it was a standard address...
        String maskString = Integer.toString((shiftby >> 24) & 255) + "." + Integer.toString((shiftby >> 16) & 255) + "." + Integer.toString((shiftby >> 8) & 255) + "." + Integer.toString(shiftby & 255);
        // Return the address thus created...
        return InetAddress.getByName(maskString);
    }
        catch(Exception e){e.printStackTrace();
    }
    // Something went wrong here...
    return null;
}

你只需要用你想要使用的IP和前缀调用它,它就会为你生成网络掩码。

icnyk63a

icnyk63a6#

我刚刚完成了一个用Java划分网络子网的API。
https://launchpad.net/subnettingapi
它具有这些功能和更多功能。

hgqdbh6s

hgqdbh6s7#

这里有一个答案,如何从WIFI连接获得子掩码:link
我根据自己的需要进行了调整,它是:

private static String intToIP(int ipAddress) {
    String ret = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
            (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
            (ipAddress >> 24 & 0xff));

    return ret;
}

public static String GetSubnetMask_WIFI() {

    WifiManager wifiManager = (WifiManager) Global.getMainActivity()
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    DhcpInfo dhcp = wifiManager.getDhcpInfo();
    String mask = intToIP(dhcp.netmask);

    return mask;
}
bt1cpqcv

bt1cpqcv8#

总之,获得掩模的方法如下:

public String mascara() throws SocketException{
    try{
        InetAddress localHost = Inet4Address.getLocalHost();
        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
        prefijo = 
            ""+networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
        int shft = 0xffffffff<<(32- 
                networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength());
        int oct1 = ((byte) ((shft&0xff000000)>>24)) & 0xff;
        int oct2 = ((byte) ((shft&0x00ff0000)>>16)) & 0xff;
        int oct3 = ((byte) ((shft&0x0000ff00)>>8)) & 0xff;
        int oct4 = ((byte) (shft&0x000000ff)) & 0xff;
        mascara = oct1+"."+oct2+"."+oct3+"."+oct4;
        // System.out.println(""+mascara);           
    }catch(UnknownHostException e){
        System.out.println("Error: "+e);
    }
    return mascara;
}
kr98yfug

kr98yfug9#

FWIW,过去我尝试使用InterfaceAddress.getNetworkPrefixLength()和InterfaceAddress.getBroadcast(),但它们不返回准确的信息(这是在Windows上,Sun JDK 1.6.0更新10)。网络前缀长度是128(不是24,它在我的网络上),返回的广播地址是255.255.255.255(不是192.168.1.255,它在我的网络上)。网络)。
詹姆斯
更新:我刚刚找到了发布在这里的解决方案:

http://forums.sun.com/thread.jspa?threadID=5277744

您需要阻止Java使用IPv6,这样它就不会通过IPv6到达IPv4。在命令行中添加-Djava.net.preferIPv4Stack=true可以修复InterfaceAddress.getNetworkPrefixLength()和InterfaceAddress.getBroadcast()的结果。

相关问题