How to know which IP is after a certain number of IPs - python

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

Related

Calculate which subnets an IP address belongs to

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.

Trying to replace last 2 octets of ip address

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

IPv6 address assignment in the 0::/8 range

I need to know if the address in the range 0::/96 can be actually assigned in IPv6 or not.
I've found the reference on IANA that IANA can't assign that range (actually 0::/8 range) but I can't find it as being an actually "reserved" range.
My issue is that I'm converting IP Addresses from integers on python. Using the standard library ipaddress which has a convenient factory method ip_address that applies the simple heuristic, if n < 2**32 then ipv4 else ipv6.
This heurisitc would be great if I could find a place in which it screams out to networks admins to forbid using this range xD
Anyway, thanks!
You can find what you are looking for in RFC 5156. Section 2.3 lists the "IPv4-Compatible Addresses" which have been deprecated:
These addresses are deprecated and should not appear on the public Internet
And if you should see them they represent an IPv4 address (except for ::1), so a n > 1 && n < 2**32 heuristic should be perfectly safe.

How to check an IP address is within a predefined list in python

Providing that I have this list which contains a number IP addresses:
IpAddresses = ["192.168.0.1","192.168.0.2","192.168.0.3","192.168.0.4"]
Then after receiving a packet I want to check if its source address is included in the predefined list IpAddresses
data, address = rxsocket.recvfrom(4096)
I have tried two alternatives, but both didn't work:
First:
if (address in IpAddresses):
do something
Then, I tried to convert address into string before making the comparison:
str_address = str(address)
if (str_address in IpAddresses):
do something
I am not familiar with python syntax, so please could you show me how to do this.
if address[0] in IpAddresses:
since the address object appears as a tuple only the 0th index appears in your list so you should check for its existence (also you can usually skip the parenthesis on an if statement unless it makes the if statement less readable)

Referencing range of IP addresses

I am trying to specify a range of addresses that will be set every time an API is called. For the example below, when api is referenced, I would like it to hosts in the range to a list, and not just one as it currently does.
api = xmlrpclib.ServerProxy("http://user:pass#192.168.0.1:8442/")
Generating the addresses seems straightforward enough, but I am unsure how to store it so that when api is reference, it's sends to every host, e.g. 192.168.0.1 - 192.168.0.100 and not just one.
for i in range(100):
ip = "192.168.0.%d" % (i)
print ip
I would also like to be able to specify the range, e.g. 192.168.0.5 - 192.168.0.50 rather then incrementing from zero.
Update: The API does not handle a list very well so the solution need to be able to parse the list. Might this simply require a second for statement?
If you want a different range:
for i in range(5,51):
ip = "192.168.0.%d" % (i)
print ip
Not sure what you mean by setting multiple. That for loop is doing that for you. If you're talking about saving references of your api, you can also throw those into a list.
api = []
for i in xrange(5,51):
ip = "192.168.0.%d" % (i)
api.append(xmlrpclib.ServerProxy("http://user:pass#" + ip))

Categories