Search code examples
ripcidr

Compare Multiple IP Addresses to Multiple CIDRS in R


I want to determine what CIDRs my IP addresses match. I have tried using iptools but the vectors are not the same size.

Example: 192.168.100.10
CIDR match: 192.168.100.0/24 or 192.168.0.0/16

Solution

  • Until a more optimized method comes along, you can totally do this with iptools (with some elbow grease):

    library(iptools)
    library(purrr)
    
    ips <- "192.168.100.10"
    
    cidrs <- c("192.168.100.0/24", "192.168.0.0/16")
    
    map_df(ips, ~{
      map2_dfr(.x, cidrs, ~{
        ips_in_cidrs(.x, .y) %>% 
          mutate(cidr = .y)
      })
    }) 
    ## # A tibble: 2 x 3
    ##   ips            in_cidr cidr            
    ##   <chr>          <lgl>   <chr>           
    ## 1 192.168.100.10 TRUE    192.168.100.0/24
    ## 2 192.168.100.10 TRUE    192.168.0.0/16