-4

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
Ironcache
  • 1,719
  • 21
  • 33
Aaron W
  • 1
  • 4
  • In the future, abiding by the guidelines presented when opening a question and in the SO R FAQ (i.e. showing some code that didn't work) would avoid the folks ticking the downvote and close flag. (I have not done that but mostly b/c I'm the co-author of `iptools`) – hrbrmstr May 10 '18 at 22:14
  • I appreciate it! I had found your iptools which is different from the CRAN version, I however could not install properly in my work environment. I figured out a workaround and your package is working great, thank you! – Aaron W May 11 '18 at 15:15

1 Answers1

1

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 
Ironcache
  • 1,719
  • 21
  • 33
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205