I'm kind of stuck on a particular issue. I need to calculate the subnets an IP address belongs to. It could be done in SQL or Python or similar.
So if I have e.g. 100.100.100.105 I need to get all the subnets:
100.100.100.105/32
100.100.100.104/31
100.100.100.104/30
100.100.100.104/29
100.100.100.96/28
100.100.100.96/27
100.100.100.64/26
...
100.64.0.0/10
100.0.0.0/9
...
64.0.0.0/2
0.0.0.0/1
But really don't know how to approach the issue.
you can achieve this with the python3 built in ipaddress module
import ipaddress
addresses = []
# get the int representation of your ip
ip = int(ipaddress.IPv4Address('100.100.100.105'))
for mask in range(32):
addresses.append(f'{ipaddress.IPv4Address(ip & ((2**32-1)-(2**mask -1)))}/{32-mask }')
At each iteration, we apply the mask to our IP by doing a logic AND operation between the integer representations of the IP and the mask. We then convert the result to ip octets and append the '/24' subnet notation. You can replace 2**32-1 with 4294967295, I left it there so its clearer what is happening.
Related
I have the following ip address "192.168.2.65"
Is there a way to convert the last 2 octets to 0.
I found the following, but it only lets me replace the last one, i need to replace the last 2.
ip = 192.168.2.65
output='.'.join(ip.split('.')[:-1]+["0"])
print(output)
which gives me 192.168.2.0 and i would like to be 192.168.0.0
Index -1 means the last index. If you want to change two, change your index to -2.
output='.'.join(ip.split('.')[:-2]+["0", "0"])
You could also use a regex based approach here:
ip = "192.168.2.65"
output = re.sub(r'\.\d+\.\d+$', '.0.0', ip)
print(output) # prints 192.168.0.0
Dependant on the logic you are trying to apply.. if you are simply wanting to modify a string, the other answers are correct.
However, if you are looking to get the network address for the subnet an address resides in, you should handle the addresses correctly and use the ipaddress module.
This will assist in calculating the correct network & broadcast addresses, and allow you to check inclusions in networks etc.
import ipaddress
interface = IPv4Interface('192.168.2.35/255.255.0.0')
print(interface.network)
#192.168.0.0/16
print(interface.network.network_address)
#192.168.0.0
print(interface.network.broadcast_address)
#192.168.255.255
Im working on RIPE Delegation Stats
and each line in the file is in the following format:
registry|cc|type|start|value|date|status[|extensions...]
where start and value means the following:
start: This is the IPv4 'first address' of the range.
value: In the case of IPv4 address the count of hosts for this range. This count does not have to represent a CIDR range.
examples for start and value:
196.4.163.0|768
41.74.0.0|4096
195.35.104.64|192
198.54.38.0|1536
216.252.155.0|256
141.226.144.0|10240
93.242.1.0|20224
193.53.200.0|11520
How can I know which IP to end up with?
Thank you so much!
--- Update ----
I figured out how to solve this problem.
convert the first IP to a decimal number then add the value to it then convert back to ip < like that I will get both start and end IP for the range
ipcalc, sipcalc, or just pure Bash will do the trick.
For more answers, including with examples, see: https://serverfault.com/q/54981
As for Python, you can just use the builtin https://docs.python.org/3/library/ipaddress.html or just struct and socket builtins https://stackoverflow.com/a/44043448
I have an ip address 1.2.3.4 with a subnet mask 255.255.255.0
I want to convert this to cidr notation
1.2.3.4/24
How do I do this in Python3?
Use the ipaddress module in the standard library.
An address plus a netmask is either a network or an interface, not an address. Given that you've got some of the host bits set (it's 1.2.3.4, not 1.2.3.0), either you've got an interface, or you've got a non-canonical name for a network; I'll assume it's an interface, so use ip_interface:
>>> i = ipaddress.ip_interface('1.2.3.4/255.255.255.0')
Or, if you want to make sure it's explicitly IPv4 not IPv6:
>>> i = ipaddress.IPv4Interface('1.2.3.4/255.255.255.0')
Or you can compose it out of an address and a network, instead of out of a combined string. It depends on what format you have this information in and what makes sense to you.
To get the CIDR format, use the with_prefixlen accessor:
>>> i.with_prefixlen
'1.2.3.4/24'
You can also do all kinds of other nifty things—extract the address (1.2.3.4) as i.address, or the network (1.2.3.0/24) as i.network, or enumerate all the addresses on the network by treating i.network as a sequence, etc.
You can use the IPy library to do this. If you scroll down to the documentation you can see the string conversions it can do. The one we're after is strNormal(1)
IP("1.2.3.4/255.255.255.0").strNormal(1)
I am questioning the results of the ipcalc module (ipcalc) for Python (it seems that netaddr may be a better choice).
Let's take 192.168.1.25/30 as an example. In binary, the last octet is 00011001 AND 11111100 = 00011000, so I get 192.168.1.24 as the Network ID and the range 192.168.1.24 - 192.168.1.27.
Using ipcalc, when I specify
subnet = ipcalc.Network('192.168.1.25/30')
for x in subnet: print x
The output is
192.168.1.25
192.168.1.26
192.168.1.27
192.168.1.28
I am not understanding the inconsistency. When using CIDR notation, it seems that specifying both 192.168.1.24/30 and 192.168.1.25/30 (or .26/30 or .27/30) refer to the same subnet.
Is that correct? Is this just a bug in the ipcalc module?
There is an open bug for this at the moment: No way to resolve IP + Netmask to Network Object
And an earlier bug report that discuss the matter: Strange subnet calculations
But they have also added a function called network to get the network address from an IP. From the manual:
>>> localnet = Network('127.128.99.3/8')
>>> print localnet.network()
127.0.0.0
The manual specifically says that the constructor Network should take a network address as its first argument, not any IP in the network. Rather confusing if you ask me (especially since the above code block breaks that condition). I would at least read the code for the module before using it.
It's correct the subnet is 192.168.1.24/30 so the ips 192.168.1.24 to 192.168.1.27 make part of this network.
Given the range xxx.xxx.xxx.(195-223)
Is that correct to write it in xxx.xxx.xxx.196/29 and check whether an IP is in the given network by doing
from ipaddr import IP, CIDR
#if IP('xxx.xxx.xxx.xxx') in IP('xxx.xxx.xxx.196/29') or
#if IP('xxx.xxx.xxx.xxx') in CIDR('xxx.xxx.xxx.196/29')
I didn't see IP in ipaddr, only IPAddress.
May be like this?
from ipaddr import IPAdddress, IPNetwork
if IPAddress('10.0.0.195') in IPNetwork('10.0.0.196/29'):
pass
I haven't used the ipaddr module, but note that /29 means that your network mask is 255.255.255.248 and that you only have the latest 3 bits to address 2^3 = 8 different hosts in your network ranging from xxx.xxx.xxx.248 to xxx.xxx.xxx.255. That's outside of the range you want to check.
For more information, please have a look at the subnetwork wikipedia page.