Learn R Programming

iptools (version 0.7.2)

ip_to_subnet: Create an IPv4 network from an address and prefix length.

Description

Given a character vector of IP addresses in CIDR notation (e.g. 1.2.3.4/24) or a character vector of IP addresses and an integer vector of prefix lengths, return a character vector of the network (in CIDR notation).

Usage

ip_to_subnet(ip_addresses, prefix_lengths = NULL)

Arguments

ip_addresses

either a character vector of IP addresses in CIDR notation (e.g. 1.2.3.4/24) --- in which case prefix_lengths should be NULL --- or a character vector of IP addresses --- in which case prefix_lengths should be a character vector of the same length as ip_addresses.

prefix_lengths

should be NULL (the default) if ip_addresses is a a character vector of IP addresses in CIDR notation otherwise should be a character vector of the same length as ip_addresses.

Details

Suggested by Slava Nikitin (https://github.com/hrbrmstr/iptools/issues/38).

Examples

Run this code
# NOT RUN {
host_ip <- c("1.2.3.4", "4.3.2.1")
subnet_len <- c(24L, 25L)
ip_to_subnet(host_ip, subnet_len)
ip_to_subnet(c("1.2.3.4/24", "4.3.2.1/25"))
# }

Run the code above in your browser using DataLab