🛠️ToolsShed

IPサブネット計算機

CIDRブロックのサブネットマスク、ネットワークアドレス、ブロードキャスト、ホスト範囲を計算します。

よくある質問

コード実装

import ipaddress

# Parse a network in CIDR notation
network = ipaddress.ip_network('192.168.1.0/24', strict=False)

print(f"Network address : {network.network_address}")   # 192.168.1.0
print(f"Broadcast addr  : {network.broadcast_address}") # 192.168.1.255
print(f"Subnet mask     : {network.netmask}")           # 255.255.255.0
print(f"Prefix length   : /{network.prefixlen}")        # /24
print(f"Total addresses : {network.num_addresses}")     # 256
print(f"Usable hosts    : {network.num_addresses - 2}") # 254

# Iterate over host addresses (excludes network and broadcast)
for host in list(network.hosts())[:3]:
    print(host)  # 192.168.1.1, .2, .3 ...

# Check if an IP is in the subnet
ip = ipaddress.ip_address('192.168.1.42')
print(ip in network)  # True

# Split into subnets
for subnet in network.subnets(prefixlen_diff=1):  # two /25 subnets
    print(subnet)

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.