Calculate an IPv6 range from a CIDR prefix?

First of all: IPv6 doesn’t have network and broadcast addresses. You can use all addresses in a prefix. Second: On a LAN the prefix length is always (well, 99.x% of the time) a /64. Routing a /68 would break IPv6 features like stateless auto configuration. Below is a verbose implementation of an IPv6 prefix calculator: … Read more

Getting list IPs from CIDR notation in PHP

I’ll edit the aforementioned class to contain a method for that. Here is the code I came up with that might help you until then. function cidrToRange($cidr) { $range = array(); $cidr = explode(“https://stackoverflow.com/”, $cidr); $range[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 – (int)$cidr[1])))); $range[1] = long2ip((ip2long($range[0])) + pow(2, (32 – (int)$cidr[1])) – 1); return … Read more

Converting CIDR address to subnet mask and network address

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);

How can I generate all possible IPs from a list of ip ranges in Python?

This function returns all ip addresses like from start to end: def ips(start, end): import socket, struct start = struct.unpack(‘>I’, socket.inet_aton(start))[0] end = struct.unpack(‘>I’, socket.inet_aton(end))[0] return [socket.inet_ntoa(struct.pack(‘>I’, i)) for i in range(start, end)] These are the building blocks to build it on your own: >>> import socket, struct >>> ip = ‘0.0.0.5’ >>> i = … Read more

How can I check if an ip is in a network in Python?

Using ipaddress (in the stdlib since 3.3, at PyPi for 2.6/2.7): >>> import ipaddress >>> ipaddress.ip_address(‘192.168.0.1’) in ipaddress.ip_network(‘192.168.0.0/24′) True If you want to evaluate a lot of IP addresses this way, you’ll probably want to calculate the netmask upfront, like n = ipaddress.ip_network(‘192.0.0.0/16’) netw = int(n.network_address) mask = int(n.netmask) Then, for each address, calculate the … Read more