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

Check whether or not a CIDR subnet contains an IP address

If only using IPv4: use ip2long() to convert the IPs and the subnet range into long integers convert the /xx into a subnet mask do a bitwise ‘and’ (i.e. ip & mask)’ and check that that ‘result = subnet’ something like this should work: function cidr_match($ip, $range) { list ($subnet, $bits) = explode(“https://stackoverflow.com/”, $range); if … 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