CIDR Subnet Calculator
Calculate network address, broadcast, host range, and subnet mask from CIDR notation.
Frequently Asked Questions
Code Implementation
import ipaddress
def cidr_info(cidr: str) -> dict:
network = ipaddress.ip_network(cidr, strict=False)
return {
"network_address": str(network.network_address),
"broadcast_address": str(network.broadcast_address),
"subnet_mask": str(network.netmask),
"prefix_length": network.prefixlen,
"total_addresses": network.num_addresses,
"usable_hosts": max(0, network.num_addresses - 2),
"first_host": str(network.network_address + 1),
"last_host": str(network.broadcast_address - 1),
}
# Example
info = cidr_info("192.168.1.0/24")
for k, v in info.items():
print(f"{k}: {v}")
# Check if IP is in network
net = ipaddress.ip_network("10.0.0.0/8", strict=False)
ip = ipaddress.ip_address("10.5.20.1")
print(f"10.5.20.1 in 10.0.0.0/8: {ip in net}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.