将 CIDR 地址转换为子网掩码和网络地址

发布于 2024-09-03 18:01:14 字数 198 浏览 15 评论 0原文

给定一个CIDR地址,例如192.168.10.0/24

  • 如何确定掩码长度? (24)
  • 如何确定掩码地址? (255.255.255.0)
  • 如何确定网络地址? (<代码>192.168.10.0)

Given a CIDR address, e.g. 192.168.10.0/24

  • How to determine mask length? (24)
  • How to determine mask address? (255.255.255.0)
  • How to determine network address? (192.168.10.0)

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

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

发布评论

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

评论(11

被你宠の有点坏 2024-09-10 18:01:14

它被 apache utils 覆盖。

请参阅此 URL:http://commons.apache。 org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html

String subnet = "192.168.0.3/31";
SubnetUtils utils = new SubnetUtils(subnet);

utils.getInfo().isInRange(address)

注意:例如,要使用 /32 CIDR 子网,需要添加以下声明:

utils.setInclusiveHostCount(true);

It is covered by apache utils.

See this URL: http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html

String subnet = "192.168.0.3/31";
SubnetUtils utils = new SubnetUtils(subnet);

utils.getInfo().isInRange(address)

Note: For use w/ /32 CIDR subnets, for exemple, one needs to add the following declaration :

utils.setInclusiveHostCount(true);
只有一腔孤勇 2024-09-10 18:01:14

这就是你在 Java 中的做法,

    String[] parts = addr.split("/");
    String ip = parts[0];
    int prefix;
    if (parts.length < 2) {
        prefix = 0;
    } else {
        prefix = Integer.parseInt(parts[1]);
    }
    int mask = 0xffffffff << (32 - prefix);
    System.out.println("Prefix=" + prefix);
    System.out.println("Address=" + ip);

    int value = mask;
    byte[] bytes = new byte[]{ 
            (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) };

    InetAddress netAddr = InetAddress.getByAddress(bytes);
    System.out.println("Mask=" + netAddr.getHostAddress());

This is how you would do it in Java,

    String[] parts = addr.split("/");
    String ip = parts[0];
    int prefix;
    if (parts.length < 2) {
        prefix = 0;
    } else {
        prefix = Integer.parseInt(parts[1]);
    }
    int mask = 0xffffffff << (32 - prefix);
    System.out.println("Prefix=" + prefix);
    System.out.println("Address=" + ip);

    int value = mask;
    byte[] bytes = new byte[]{ 
            (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) };

    InetAddress netAddr = InetAddress.getByAddress(bytes);
    System.out.println("Mask=" + netAddr.getHostAddress());
躲猫猫 2024-09-10 18:01:14

IPAddress Java 库 以多态方式支持 IPv4 和 IPv6,包括子网。 javadoc 可在链接中找到。免责声明:我是项目经理。

您列出的所有用例均支持 IPv4 和 Ipv6。

    String str = "192.168.10.0/24";
    IPAddressString addrString = new IPAddressString(str);
    try {
         IPAddress addr = addrString.toAddress();
         Integer prefix = addr.getNetworkPrefixLength(); //24
         IPAddress mask = addr.getNetwork().getNetworkMask(prefix, false);//255.255.255.0
         IPAddress networkAddr = addr.mask(mask);  //192.168.10.0
         IPAddress networkAddrOtherWay = addr.getLower().removePrefixLength(); //192.168.10.0

         ...
    } catch(AddressStringException e) {
        //e.getMessage provides validation issue
    }

The IPAddress Java library supports both IPv4 and IPv6 in a polymorphic manner including subnets. The javadoc is available at the link. Disclaimer: I am the project manager.

All the use cases you listed are supported for both IPv4 and Ipv6 transparently.

    String str = "192.168.10.0/24";
    IPAddressString addrString = new IPAddressString(str);
    try {
         IPAddress addr = addrString.toAddress();
         Integer prefix = addr.getNetworkPrefixLength(); //24
         IPAddress mask = addr.getNetwork().getNetworkMask(prefix, false);//255.255.255.0
         IPAddress networkAddr = addr.mask(mask);  //192.168.10.0
         IPAddress networkAddrOtherWay = addr.getLower().removePrefixLength(); //192.168.10.0

         ...
    } catch(AddressStringException e) {
        //e.getMessage provides validation issue
    }
世界等同你 2024-09-10 18:01:14

根据尤里的回答:
要获取整个 IP 地址范围,Apache Java 类 SubnetUtils 提供了以下方法:

String[] addresses = utils.getInfo().getAllAddresses();

要下载包含该类的 jar,请访问:
http://repo1.maven.org/ maven2/commons-net/commons-net/3.0.1/commons-net-3.0.1.jar

源代码:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java?view=markup

Maven ID:

<groupId>commons-net<groupId>
<artifactId>commons-net<artifactId>
<version>3.0.1<version>

Following Yuriy's answer:
To get the whole range of ip addresses, the Apache Java class SubnetUtils offers the following methods:

String[] addresses = utils.getInfo().getAllAddresses();

To download the jar containing the class go to:
http://repo1.maven.org/maven2/commons-net/commons-net/3.0.1/commons-net-3.0.1.jar

The source code:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java?view=markup

Maven id:

<groupId>commons-net<groupId>
<artifactId>commons-net<artifactId>
<version>3.0.1<version>
不回头走下去 2024-09-10 18:01:14

Linux 命令行 ipcalc。
您可以快速使用:

$ipcalc 192.168.10.0/24
Address:   192.168.10.0         11000000.10101000.00001010. 00000000
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.10.0/24      11000000.10101000.00001010. 00000000
HostMin:   192.168.10.1         11000000.10101000.00001010. 00000001
HostMax:   192.168.10.254       11000000.10101000.00001010. 11111110
Broadcast: 192.168.10.255       11000000.10101000.00001010. 11111111
Hosts/Net: 254                   Class C, Private Internet

Linux command line ipcalc.
You can quickly use :

$ipcalc 192.168.10.0/24
Address:   192.168.10.0         11000000.10101000.00001010. 00000000
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.10.0/24      11000000.10101000.00001010. 00000000
HostMin:   192.168.10.1         11000000.10101000.00001010. 00000001
HostMax:   192.168.10.254       11000000.10101000.00001010. 11111110
Broadcast: 192.168.10.255       11000000.10101000.00001010. 11111111
Hosts/Net: 254                   Class C, Private Internet
ゞ记忆︶ㄣ 2024-09-10 18:01:14

该算法是伪代码(实际上是PHP),你可以自己将其翻译成java。
来自此处的算法。

//$ipNetmask = "192.168.1.12/30";
list($ip, $netmask) = split( "/", $ipNetmask );
$ip_elements_decimal = split( "[.]", $ip );
$netmask_result="";
for($i=1; $i <= $netmask; $i++) {
  $netmask_result .= "1";
}
for($i=$netmask+1; $i <= 32; $i++) {
    $netmask_result .= "0";
}
$netmask_ip_binary_array = str_split( $netmask_result, 8 );
$netmask_ip_decimal_array = array();
foreach( $netmask_ip_binary_array as $k => $v ){
    $netmask_ip_decimal_array[$k] = bindec( $v ); // "100" => 4
    $network_address_array[$k] = ( $netmask_ip_decimal_array[$k] & $ip_elements_decimal[$k] );
}
$network_address = join( ".", $network_address_array );

// ------------------------------------------------
           // TCP/IP NETWORK INFORMATION
// ------------------------------------------------
// IP Entered = ..................: 192.168.1.12
// CIDR = ........................: /30
// Netmask = .....................: 255.255.255.252
// Network Address = .............: 192.168.1.12

// Broadcast Address = ...........: 192.168.1.15
// Usable IP Addresses = .........: 2
// First Usable IP Address = .....: 192.168.1.13
// Last Usable IP Address = ......: 192.168.1.14

The algorithm is in pseudo code (actually PHP), you can translate it to java yourself.
Algoritm from here.

//$ipNetmask = "192.168.1.12/30";
list($ip, $netmask) = split( "/", $ipNetmask );
$ip_elements_decimal = split( "[.]", $ip );
$netmask_result="";
for($i=1; $i <= $netmask; $i++) {
  $netmask_result .= "1";
}
for($i=$netmask+1; $i <= 32; $i++) {
    $netmask_result .= "0";
}
$netmask_ip_binary_array = str_split( $netmask_result, 8 );
$netmask_ip_decimal_array = array();
foreach( $netmask_ip_binary_array as $k => $v ){
    $netmask_ip_decimal_array[$k] = bindec( $v ); // "100" => 4
    $network_address_array[$k] = ( $netmask_ip_decimal_array[$k] & $ip_elements_decimal[$k] );
}
$network_address = join( ".", $network_address_array );

// ------------------------------------------------
           // TCP/IP NETWORK INFORMATION
// ------------------------------------------------
// IP Entered = ..................: 192.168.1.12
// CIDR = ........................: /30
// Netmask = .....................: 255.255.255.252
// Network Address = .............: 192.168.1.12

// Broadcast Address = ...........: 192.168.1.15
// Usable IP Addresses = .........: 2
// First Usable IP Address = .....: 192.168.1.13
// Last Usable IP Address = ......: 192.168.1.14
記柔刀 2024-09-10 18:01:14

您可以使用 Spring Framework 中的 org.springframework.security.web.util.IpAddressMatcher 。

You can use org.springframework.security.web.util.IpAddressMatcher from Spring Framework.

寄居人 2024-09-10 18:01:14

这是我的 groovy 的:)

//IP calculator by ku1gun

// input
String inputAddr = "12.34.56.78/20";

//magic
def(String ipAddrBin, String maskAddrBin, String invertedMaskBin, int hostsCount) = getIpAddrAndCidrMaskBin(inputAddr);

String broadcastAddr = retrieveBroadcastAddr(ipAddrBin, invertedMaskBin);
String ipAddr = getTenBaseAddrValueFromBin(ipAddrBin);
String maskAddr = getTenBaseAddrValueFromBin(maskAddrBin);
String invertedMask = getTenBaseAddrValueFromBin(invertedMaskBin);
String networkAddr = retrieveNetworkAddr(ipAddrBin, maskAddrBin);

def (String ipMinVal, String ipMaxVal) = getMinMaxIpRangeValues(networkAddr, broadcastAddr)

//Output "debug" results
System.out.println("Variables:");
System.out.println("ipInput: " + ipAddr);
System.out.println("MaskInput: " + maskAddr);
System.out.println("invertedMask: " + invertedMask);
System.out.println("-----------------------");
System.out.println("Binaries:");
System.out.println("ipBin: " + ipAddrBin);
System.out.println("MaskInBin: " + maskAddrBin);
System.out.println("InvertedMaskBin: " + invertedMaskBin);
System.out.println("-----------------------");
System.out.println("Results:");
System.out.println("maskAddr: " + maskAddr);
System.out.println("hostsCount: " + hostsCount);
System.out.println("networkAddr: " + networkAddr);
System.out.println("broadcastAddr: " + broadcastAddr);
System.out.println("ipMinVal: " + ipMinVal);
System.out.println("ipMaxVal: " + ipMaxVal);
System.out.println("-----------------------");
System.out.println("IP range list:");

long ipStart = host2long(ipMinVal);
long ipEnd = host2long(ipMaxVal);


for (long i=ipStart; i<=ipEnd; i++) 
{
    System.out.println(long2dotted(i));
}


//general methods
def getIpAddrAndCidrMaskBin(String inputAddrStr)
{
    def netMask = "";
    def invNetMask = "";

    def cidrAddressList = inputAddrStr.tokenize("\\/")
    def baseIPAddress = cidrAddressList.first()
    def cidrIPMask = cidrAddressList.last().toInteger()

    //retrieve binaryNetMask and binaryInvertedNetMask
    for(i=0; i<32; i++)
    {
        if(i<cidrIPMask)
        {
            netMask = netMask + "1";
            invNetMask = invNetMask + "0";
        }
        else
        {
            netMask = netMask + "0";
            invNetMask = invNetMask + "1";
        }
    }

    //retrieve binaryIpAddress
    String[] addrOctetArray = baseIPAddress.split("\\.");
    String binAddr = "";
    for (String string : addrOctetArray)
        {
            int octet = Integer.parseInt(string);     
            String binaryOctet = String.format("%8s", Integer.toBinaryString(octet)).replace(' ', '0');
            binAddr = binAddr + binaryOctet;
        }

    hostsCount = 2**(32 - cidrIPMask) - 2;

    return [binAddr, netMask, invNetMask, hostsCount]
}

def getTenBaseAddrValueFromBin(String binVal)
{
    tenBaseAddr = "";
    tenBaseAddr = tenBaseAddr + Integer.parseInt(binVal.substring(0,8), 2) + "." + Integer.parseInt(binVal.substring(8,16), 2) + "." + Integer.parseInt(binVal.substring(16,24), 2) + "." + Integer.parseInt(binVal.substring(24,32), 2)
    return tenBaseAddr;
}

def retrieveBroadcastAddr(String ipAddrBin, String invertedMaskBin)
{
    def oct_1 = Integer.parseInt(ipAddrBin.substring(0,8), 2) | Integer.parseInt(invertedMaskBin.substring(0,8), 2);
    def oct_2 = Integer.parseInt(ipAddrBin.substring(8,16), 2) | Integer.parseInt(invertedMaskBin.substring(8,16), 2);
    def oct_3 = Integer.parseInt(ipAddrBin.substring(16,24), 2) | Integer.parseInt(invertedMaskBin.substring(16,24), 2);
    def oct_4 = Integer.parseInt(ipAddrBin.substring(24,32), 2) | Integer.parseInt(invertedMaskBin.substring(24,32), 2);

    def t_oct = oct_1 + "."+ oct_2 + "." +  oct_3 + "." + oct_4;
    return t_oct
}

def retrieveNetworkAddr(String ipAddrBin, String maskInBin)
{
    def oct_1 = Integer.parseInt(ipAddrBin.substring(0,8), 2) & Integer.parseInt(maskInBin.substring(0,8), 2);
    def oct_2 = Integer.parseInt(ipAddrBin.substring(8,16), 2) & Integer.parseInt(maskInBin.substring(8,16), 2);
    def oct_3 = Integer.parseInt(ipAddrBin.substring(16,24), 2) & Integer.parseInt(maskInBin.substring(16,24), 2);
    def oct_4 = Integer.parseInt(ipAddrBin.substring(24,32), 2) & Integer.parseInt(maskInBin.substring(24,32), 2);

    def t_oct = oct_1 + "."+ oct_2 + "." +  oct_3 + "." + oct_4;
    return t_oct
}

def getMinMaxIpRangeValues(networkAddr, broadcastAddr)
{
    String[] ipAddrOctetArray = networkAddr.split("\\.");
    String[] broadcastOctetArray = broadcastAddr.split("\\.");

    String minRangeVal = ipAddrOctetArray[0] + "." + ipAddrOctetArray[1] + "." + ipAddrOctetArray[2] + "." + (Integer.parseInt(ipAddrOctetArray[3]) + 1)
    String maxRangeVal = broadcastOctetArray[0] + "." +broadcastOctetArray[1] + "." +broadcastOctetArray[2] + "." + (Integer.parseInt(broadcastOctetArray[3]) - 1)

    return[minRangeVal, maxRangeVal]
}

//IP list generate
public static long host2long(String host) 
{
    long ip=0;
    if (!Character.isDigit(host.charAt(0))) return -1;
    int[] addr = ip2intarray(host);
    if (addr == null) return -1;
    for (int i=0;i<addr.length;++i) 
    {
        ip += ((long)(addr[i]>=0 ? addr[i] : 0)) << 8*(3-i);
    }
    return ip;
}

public static int[] ip2intarray(String host) 
{
    Integer[] address = [-1,-1,-1,-1];
    int i=0;
    StringTokenizer tokens = new StringTokenizer(host,".");
    if (tokens.countTokens() > 4) return null;
    while (tokens.hasMoreTokens()) 
    {
        try 
        {
            address[i++] = Integer.parseInt(tokens.nextToken()) & 0xFF;
        } 
        catch(NumberFormatException nfe) 
        {
            return null;
        }
    }
    return address;
}

public static String long2dotted(long ip) 
{
    // if ip is bigger than 255.255.255.255 or smaller than 0.0.0.0
    if (ip > 4294967295l || ip < 0) 
    {
        throw new IllegalArgumentException("invalid ip");
    }
    StringBuilder ipAddress = new StringBuilder();
    for (int i = 3; i >= 0; i--) {
        int shift = i * 8;
        ipAddress.append((ip & (0xff << shift)) >> shift);
        if (i > 0) {
            ipAddress.append(".");
        }
    }
    return ipAddress.toString();
}

this is my groovy's:)

//IP calculator by ku1gun

// input
String inputAddr = "12.34.56.78/20";

//magic
def(String ipAddrBin, String maskAddrBin, String invertedMaskBin, int hostsCount) = getIpAddrAndCidrMaskBin(inputAddr);

String broadcastAddr = retrieveBroadcastAddr(ipAddrBin, invertedMaskBin);
String ipAddr = getTenBaseAddrValueFromBin(ipAddrBin);
String maskAddr = getTenBaseAddrValueFromBin(maskAddrBin);
String invertedMask = getTenBaseAddrValueFromBin(invertedMaskBin);
String networkAddr = retrieveNetworkAddr(ipAddrBin, maskAddrBin);

def (String ipMinVal, String ipMaxVal) = getMinMaxIpRangeValues(networkAddr, broadcastAddr)

//Output "debug" results
System.out.println("Variables:");
System.out.println("ipInput: " + ipAddr);
System.out.println("MaskInput: " + maskAddr);
System.out.println("invertedMask: " + invertedMask);
System.out.println("-----------------------");
System.out.println("Binaries:");
System.out.println("ipBin: " + ipAddrBin);
System.out.println("MaskInBin: " + maskAddrBin);
System.out.println("InvertedMaskBin: " + invertedMaskBin);
System.out.println("-----------------------");
System.out.println("Results:");
System.out.println("maskAddr: " + maskAddr);
System.out.println("hostsCount: " + hostsCount);
System.out.println("networkAddr: " + networkAddr);
System.out.println("broadcastAddr: " + broadcastAddr);
System.out.println("ipMinVal: " + ipMinVal);
System.out.println("ipMaxVal: " + ipMaxVal);
System.out.println("-----------------------");
System.out.println("IP range list:");

long ipStart = host2long(ipMinVal);
long ipEnd = host2long(ipMaxVal);


for (long i=ipStart; i<=ipEnd; i++) 
{
    System.out.println(long2dotted(i));
}


//general methods
def getIpAddrAndCidrMaskBin(String inputAddrStr)
{
    def netMask = "";
    def invNetMask = "";

    def cidrAddressList = inputAddrStr.tokenize("\\/")
    def baseIPAddress = cidrAddressList.first()
    def cidrIPMask = cidrAddressList.last().toInteger()

    //retrieve binaryNetMask and binaryInvertedNetMask
    for(i=0; i<32; i++)
    {
        if(i<cidrIPMask)
        {
            netMask = netMask + "1";
            invNetMask = invNetMask + "0";
        }
        else
        {
            netMask = netMask + "0";
            invNetMask = invNetMask + "1";
        }
    }

    //retrieve binaryIpAddress
    String[] addrOctetArray = baseIPAddress.split("\\.");
    String binAddr = "";
    for (String string : addrOctetArray)
        {
            int octet = Integer.parseInt(string);     
            String binaryOctet = String.format("%8s", Integer.toBinaryString(octet)).replace(' ', '0');
            binAddr = binAddr + binaryOctet;
        }

    hostsCount = 2**(32 - cidrIPMask) - 2;

    return [binAddr, netMask, invNetMask, hostsCount]
}

def getTenBaseAddrValueFromBin(String binVal)
{
    tenBaseAddr = "";
    tenBaseAddr = tenBaseAddr + Integer.parseInt(binVal.substring(0,8), 2) + "." + Integer.parseInt(binVal.substring(8,16), 2) + "." + Integer.parseInt(binVal.substring(16,24), 2) + "." + Integer.parseInt(binVal.substring(24,32), 2)
    return tenBaseAddr;
}

def retrieveBroadcastAddr(String ipAddrBin, String invertedMaskBin)
{
    def oct_1 = Integer.parseInt(ipAddrBin.substring(0,8), 2) | Integer.parseInt(invertedMaskBin.substring(0,8), 2);
    def oct_2 = Integer.parseInt(ipAddrBin.substring(8,16), 2) | Integer.parseInt(invertedMaskBin.substring(8,16), 2);
    def oct_3 = Integer.parseInt(ipAddrBin.substring(16,24), 2) | Integer.parseInt(invertedMaskBin.substring(16,24), 2);
    def oct_4 = Integer.parseInt(ipAddrBin.substring(24,32), 2) | Integer.parseInt(invertedMaskBin.substring(24,32), 2);

    def t_oct = oct_1 + "."+ oct_2 + "." +  oct_3 + "." + oct_4;
    return t_oct
}

def retrieveNetworkAddr(String ipAddrBin, String maskInBin)
{
    def oct_1 = Integer.parseInt(ipAddrBin.substring(0,8), 2) & Integer.parseInt(maskInBin.substring(0,8), 2);
    def oct_2 = Integer.parseInt(ipAddrBin.substring(8,16), 2) & Integer.parseInt(maskInBin.substring(8,16), 2);
    def oct_3 = Integer.parseInt(ipAddrBin.substring(16,24), 2) & Integer.parseInt(maskInBin.substring(16,24), 2);
    def oct_4 = Integer.parseInt(ipAddrBin.substring(24,32), 2) & Integer.parseInt(maskInBin.substring(24,32), 2);

    def t_oct = oct_1 + "."+ oct_2 + "." +  oct_3 + "." + oct_4;
    return t_oct
}

def getMinMaxIpRangeValues(networkAddr, broadcastAddr)
{
    String[] ipAddrOctetArray = networkAddr.split("\\.");
    String[] broadcastOctetArray = broadcastAddr.split("\\.");

    String minRangeVal = ipAddrOctetArray[0] + "." + ipAddrOctetArray[1] + "." + ipAddrOctetArray[2] + "." + (Integer.parseInt(ipAddrOctetArray[3]) + 1)
    String maxRangeVal = broadcastOctetArray[0] + "." +broadcastOctetArray[1] + "." +broadcastOctetArray[2] + "." + (Integer.parseInt(broadcastOctetArray[3]) - 1)

    return[minRangeVal, maxRangeVal]
}

//IP list generate
public static long host2long(String host) 
{
    long ip=0;
    if (!Character.isDigit(host.charAt(0))) return -1;
    int[] addr = ip2intarray(host);
    if (addr == null) return -1;
    for (int i=0;i<addr.length;++i) 
    {
        ip += ((long)(addr[i]>=0 ? addr[i] : 0)) << 8*(3-i);
    }
    return ip;
}

public static int[] ip2intarray(String host) 
{
    Integer[] address = [-1,-1,-1,-1];
    int i=0;
    StringTokenizer tokens = new StringTokenizer(host,".");
    if (tokens.countTokens() > 4) return null;
    while (tokens.hasMoreTokens()) 
    {
        try 
        {
            address[i++] = Integer.parseInt(tokens.nextToken()) & 0xFF;
        } 
        catch(NumberFormatException nfe) 
        {
            return null;
        }
    }
    return address;
}

public static String long2dotted(long ip) 
{
    // if ip is bigger than 255.255.255.255 or smaller than 0.0.0.0
    if (ip > 4294967295l || ip < 0) 
    {
        throw new IllegalArgumentException("invalid ip");
    }
    StringBuilder ipAddress = new StringBuilder();
    for (int i = 3; i >= 0; i--) {
        int shift = i * 8;
        ipAddress.append((ip & (0xff << shift)) >> shift);
        if (i > 0) {
            ipAddress.append(".");
        }
    }
    return ipAddress.toString();
}
下壹個目標 2024-09-10 18:01:14

Apache Java 类 SubnetUtils 提供了帮助来完成其中一些操作:

String[] parts = ipv4Cidr.split("/");
if (parts[1].equals("0"))
{
    // This accepts all ip addresses.  Technically not a subnet.
    maskLength = 0;
    maskAdress = "0.0.0.0"
}
else
{
     maskLength = Integer.parseInt(parts[1]);
     cidrInfo = new SubnetUtils(ipv4Cidr).getInfo();
     maskAdress = cidrInfo.asInteger(cidrInfo.getNetmask());
     networkAddress = cidrInfo.getNetworkAddress()
}

Apache Java class SubnetUtils offers help to do some of this:

String[] parts = ipv4Cidr.split("/");
if (parts[1].equals("0"))
{
    // This accepts all ip addresses.  Technically not a subnet.
    maskLength = 0;
    maskAdress = "0.0.0.0"
}
else
{
     maskLength = Integer.parseInt(parts[1]);
     cidrInfo = new SubnetUtils(ipv4Cidr).getInfo();
     maskAdress = cidrInfo.asInteger(cidrInfo.getNetmask());
     networkAddress = cidrInfo.getNetworkAddress()
}
顾挽 2024-09-10 18:01:14

这是一个简单的 Groovy 示例

def cidrAddress = '192.168.10.0/24'
def cidrAddressList = cidrAddress.tokenize("\\/")
def baseIPAddress = cidrAddressList.first()
def cidrIPMask = cidrAddressList.last().toInteger()
def netMaskList = []
Integer fullOctets = cidrIPMask.intdiv(8)
fullOctets.times {netMaskList.add('255')}
def remainder = cidrIPMask % 8
netMaskList.add((256 - (2 ** (8 - remainder))).toString())
netMaskList.addAll(['0','0','0','0'])
def netMask = netMaskList.flatten().getAt(0..3).join('.')
return [cidrAddress,baseIPAddress,cidrIPMask,netMask]

Here is a simple Groovy example

def cidrAddress = '192.168.10.0/24'
def cidrAddressList = cidrAddress.tokenize("\\/")
def baseIPAddress = cidrAddressList.first()
def cidrIPMask = cidrAddressList.last().toInteger()
def netMaskList = []
Integer fullOctets = cidrIPMask.intdiv(8)
fullOctets.times {netMaskList.add('255')}
def remainder = cidrIPMask % 8
netMaskList.add((256 - (2 ** (8 - remainder))).toString())
netMaskList.addAll(['0','0','0','0'])
def netMask = netMaskList.flatten().getAt(0..3).join('.')
return [cidrAddress,baseIPAddress,cidrIPMask,netMask]
一江春梦 2024-09-10 18:01:14
--plsql ip_calc by ku1gun
with a as
(
select
  '12.34.56.78/20' ip
from dual
),b as
(
select
  utl_raw.concat(utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,1)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,2)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,3)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(replace(ip,'/','.'),'[^.]+',1,4)),2),1,1)) ip,
  utl_raw.cast_from_binary_integer(power(2,32-to_number(regexp_substr(ip,'[^/]+',1,2)))-1) wildcard,
  utl_raw.bit_xor(utl_raw.cast_from_binary_integer(-1),utl_raw.cast_from_binary_integer(power(2,32-to_number(regexp_substr(ip,'[^/]+',1,2)))-1)) mask
from a
),c as
(
select
  utl_raw.bit_and(ip,mask) network_address,
  utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard) broadcast_address,
  utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))+1) first_adress,
  utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard))-1) last_adress,
  utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))+level) ip_address
from b
connect by level<utl_raw.cast_to_binary_integer(utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard))-utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))
)
select
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,4,1))) network_address,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,4,1))) broadcast_address,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,4,1))) first_adress,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,4,1))) last_adress,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,4,1))) ip_address
from c
;
--plsql ip_calc by ku1gun
with a as
(
select
  '12.34.56.78/20' ip
from dual
),b as
(
select
  utl_raw.concat(utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,1)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,2)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(ip,'[^.]+',1,3)),2),1,1),utl_raw.substr(utl_raw.cast_from_binary_integer(to_number(regexp_substr(replace(ip,'/','.'),'[^.]+',1,4)),2),1,1)) ip,
  utl_raw.cast_from_binary_integer(power(2,32-to_number(regexp_substr(ip,'[^/]+',1,2)))-1) wildcard,
  utl_raw.bit_xor(utl_raw.cast_from_binary_integer(-1),utl_raw.cast_from_binary_integer(power(2,32-to_number(regexp_substr(ip,'[^/]+',1,2)))-1)) mask
from a
),c as
(
select
  utl_raw.bit_and(ip,mask) network_address,
  utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard) broadcast_address,
  utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))+1) first_adress,
  utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard))-1) last_adress,
  utl_raw.cast_from_binary_integer(utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))+level) ip_address
from b
connect by level<utl_raw.cast_to_binary_integer(utl_raw.bit_or(utl_raw.bit_and(ip,mask),wildcard))-utl_raw.cast_to_binary_integer(utl_raw.bit_and(ip,mask))
)
select
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(network_address,4,1))) network_address,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(broadcast_address,4,1))) broadcast_address,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(first_adress,4,1))) first_adress,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(last_adress,4,1))) last_adress,
  to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,1,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,2,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,3,1)))||'.'||to_char(utl_raw.cast_to_binary_integer(utl_raw.substr(ip_address,4,1))) ip_address
from c
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文