Python 3: create a list of possible ip addresses from a CIDR notation

In Python 3 as simple as >>> import ipaddress >>> [str(ip) for ip in ipaddress.IPv4Network(‘192.0.2.0/28’)] [‘192.0.2.0’, ‘192.0.2.1’, ‘192.0.2.2’, ‘192.0.2.3’, ‘192.0.2.4’, ‘192.0.2.5’, ‘192.0.2.6’, ‘192.0.2.7’, ‘192.0.2.8’, ‘192.0.2.9’, ‘192.0.2.10’, ‘192.0.2.11’, ‘192.0.2.12’, ‘192.0.2.13’, ‘192.0.2.14’, ‘192.0.2.15’]

How can I determine network and broadcast address from the IP address and subnet mask?

Let’s write both in binary: 130.45.34.36 = 10000010.00101101.00100010.00100100 255.255.240.0 = 11111111.11111111.11110000.00000000 A bitwise AND between the two would give us the network address: 10000010.00101101.00100010.00100100 (ip address) AND 11111111.11111111.11110000.00000000 (subnet mask) = 10000010.00101101.00100000.00000000 = 130.45.32.0 (the resulting network address) A bitwise OR between the network address and the inverted subnet mask would give us the broadcast … Read more

How to check if an IP address is within a particular subnet

Take a look at IP Address Calculations with C# on MSDN blogs. It contains an extension method (IsInSameSubnet) that should meet your needs as well as some other goodies. public static class IPAddressExtensions { public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask) { byte[] ipAdressBytes = address.GetAddressBytes(); byte[] subnetMaskBytes = subnetMask.GetAddressBytes(); if (ipAdressBytes.Length != subnetMaskBytes.Length) … Read more

tech