Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a script that performs a search on internet for the provided IPs range. The IPs range is provided in the format xxx.xxx.xxx.x-xxx.xxx.xxx.x and I was looking to perform a search based on a given subnet xxx.xxx.xxx.x/32/24/16/8 from a file

this is the part of the script responsible for searching web

Python
# Return IPs in IPv4 range, inclusive.
def IPsRange(start='', end=''):
    if not start and not end:
        return []
    if not end and start.__contains__("-"):
        start, end = start.split("-")
    end = end.replace("\n","")
    start = int(ip_address(start).packed.hex(), 16)
    end = int(ip_address(end).packed.hex(), 16)
    return [ip_address(ip).exploded for ip in range(start, end)]

# Scan IP address range
def ScanRange(ranges):
    threads = []                        
    # *-- Scan IP range --*             
    for address in IPsRange(ranges):    
        t = Thread(                     
            target=__СheckAddrThreaded, 
            args=(address,)             
        )                               
        threads.append(t)               
        t.start()                       
    for thread in threads:              
        thread.join()


How can I can a subnet instead of a IP range?

Thank you

What I have tried:

I tried

>>> import ipaddress
>>> def get_all_ips_by_subnet(subnet):
...   l = [*map(str, ipaddress.IPv4Network(subnet, strict=False))]
...   return l
...
>>> l = get_all_ips_by_subnet("172.17.20.30/30")
>>> l
['172.17.20.28', '172.17.20.29', '172.17.20.30', '172.17.20.31']'
Posted
Updated 2-Oct-20 3:25am

1 solution

You may use iprange_to_cidrs provided by netaddr[^] module.
Starting Python 3.3 the bundled ipaddress can provide what you want. The function summarize_address_range returns an iterator with the networks resulting from the start, end you specify


To go the other way:
In Python 3 as simple as
Python
import ipaddress
[str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')]
 
Share this answer
 
v2
Comments
Member 14651994 2-Oct-20 9:47am    
@Richard Deeming
Thank you for your reply. That is a way to change the ip range to ip subnet but here I was looking on how to so that in the context of this script. I was try the script to make a search based on a subnet and not on an IP range.
Richard Deeming 2-Oct-20 9:49am    
You want a way to programmatically convert an IP range to a subnet, and the linked thread shows you how to convert an IP range to a subnet.

So your question is solved?
Member 14651994 2-Oct-20 9:55am    
What I am looking as on the question is "The IPs range is provided in the format xxx.xxx.xxx.x-xxx.xxx.xxx.x and I was looking to perform a search based on a given subnet xxx.xxx.xxx.x/32/24/16/8 from a file" its in this context and not do that conversion only.
Richard Deeming 2-Oct-20 9:57am    
According to the question, you already know how to do the search given a subnet.

You have an IP range.

You now know how to convert an IP range into a subnet.

So connect the dots...
Richard Deeming 2-Oct-20 9:58am    
Or are you saying you want to go the other way - convert a subnet to an IP range?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900