|
| 1 | +import ipaddress |
| 2 | +# initialize an IPv4 Address |
| 3 | +ip = ipaddress.IPv4Address("192.168.1.1") |
| 4 | + |
| 5 | +# print True if the IP address is global |
| 6 | +print("Is global:", ip.is_global) |
| 7 | + |
| 8 | +# print Ture if the IP address is Link-local |
| 9 | +print("Is link-local:", ip.is_link_local) |
| 10 | + |
| 11 | +# ip.is_reserved |
| 12 | +# ip.is_multicast |
| 13 | + |
| 14 | +# next ip address |
| 15 | +print(ip + 1) |
| 16 | + |
| 17 | +# previous ip address |
| 18 | +print(ip - 1) |
| 19 | + |
| 20 | +# initialize an IPv4 Network |
| 21 | +network = ipaddress.IPv4Network("192.168.1.0/24") |
| 22 | + |
| 23 | +# get the network mask |
| 24 | +print("Network mask:", network.netmask) |
| 25 | + |
| 26 | +# get the broadcast address |
| 27 | +print("Broadcast address:", network.broadcast_address) |
| 28 | + |
| 29 | +# print the number of IP addresses under this network |
| 30 | +print("Number of hosts under", str(network), ":", network.num_addresses) |
| 31 | + |
| 32 | +# iterate over all the hosts under this network |
| 33 | +print("Hosts under", str(network), ":") |
| 34 | +for host in network.hosts(): |
| 35 | + print(host) |
| 36 | + |
| 37 | +# iterate over the subnets of this network |
| 38 | +print("Subnets:") |
| 39 | +for subnet in network.subnets(prefixlen_diff=2): |
| 40 | + print(subnet) |
| 41 | + |
| 42 | +# get the supernet of this network |
| 43 | +print("Supernet:", network.supernet(prefixlen_diff=1)) |
| 44 | + |
| 45 | +# prefixlen_diff: An integer, the amount the prefix length of |
| 46 | + # the network should be decreased by. For example, given a |
| 47 | + # /24 network and a prefixlen_diff of 3, a supernet with a |
| 48 | + # /21 netmask is returned. |
| 49 | + |
| 50 | +# tell if this network is under (or overlaps) 192.168.0.0/16 |
| 51 | +print("Overlaps 192.168.0.0/16:", network.overlaps(ipaddress.IPv4Network("192.168.0.0/16"))) |
| 52 | + |
0 commit comments