Search code examples
groovyip-addresssubnet

Groovy code to fetch all available IP address from a given subnet denoted with CIDR notation


I have generated the below code with the help of Gen AI, but its returning a empty list. Can someone help me to debug this?

static List<String> generateAvailableIps(String subnetCidr) {
    // Parse the CIDR notation
    def (ipAddress, subnetMask) = subnetCidr.split("/")

    // Convert IP address to integer
    def ipInt = ipAddress.split("\\.").inject(0) { acc, value -> (acc << 8) + value.toInteger() }

    // Get subnet mask as integer
    def subnetMaskInt = (1 << (32 - subnetMask.toInteger())) - 1

    // Get network address
    def networkAddress = ipInt & subnetMaskInt

    // Get first and last usable IP addresses (excluding network and broadcast)
    def firstUsableIp = networkAddress + 1
    def broadcastAddress = networkAddress | ~subnetMaskInt
    def lastUsableIp = broadcastAddress - 1

    // Generate a list of available IP addresses
    def availableIps = []
    for (int i = firstUsableIp; i <= lastUsableIp; i++) {
        def octet1 = (i >> 24) & 0xff
        def octet2 = (i >> 16) & 0xff
        def octet3 = (i >> 8) & 0xff
        def octet4 = i & 0xff
        availableIps.add("$octet1.$octet2.$octet3.$octet4")
    }

    return availableIps
}

// Example usage
def availableIps = generateAvailableIps("192.168.35.32/27")
println(availableIps)

List of IP addresses available from a given subnet


Solution

  • static List<String> generateAvailableIps(String subnetCidr) {
        // Parse the CIDR notation
        def (ipAddress, subnetMask) = subnetCidr.split("/")
    
        // Convert IP address to integer
        def ipInt = ipAddress.split("\\.").inject(0) { acc, value -> (acc << 8) + value.toInteger() }
    
        // Get subnet mask as integer
        def subnetMaskInt = (-1 << (32 - subnetMask.toInteger()))
     
        // Get network address
        def networkAddress = ipInt & subnetMaskInt
      
        // Get first and last usable IP addresses (excluding network and broadcast)
        def broadcastAddress = networkAddress | ~subnetMaskInt
        def firstUsableIp = ""
        def lastUsableIp = ""
         
        if(subnetMask != "31")
        {
            firstUsableIp = networkAddress + 1
            lastUsableIp = broadcastAddress - 1
        }
        else
        {
            firstUsableIp = networkAddress
            lastUsableIp = broadcastAddress 
        }
        
        // Generate a list of available IP addresses
        def availableIps = []
        for (int i = firstUsableIp; i <= lastUsableIp; i++) {
            def octet1 = (i >> 24) & 0xff
            def octet2 = (i >> 16) & 0xff
            def octet3 = (i >> 8) & 0xff
            def octet4 = i & 0xff
            availableIps.add("$octet1.$octet2.$octet3.$octet4")
        }
    
        return availableIps
    }
    
    // Example usage
    def subnetCidrs = ["192.168.0.0/31","192.168.35.32/27"]
    def String ipAddress = '192.168.35.37'
    
    for (subnetCidr in subnetCidrs) {
        def availableIps = generateAvailableIps(subnetCidr)
        println(availableIps)  
    }