Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a script that performs a search on internet and that calls a file with an IP range this way

Python
# Import modules
from .inspection import Request, InspectPaths, InspectContent, PortIsOpen, GetTitle
from ipaddress import ip_address
from threading import Thread

    # 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()


I was trying to, instead of an IP range, to use an IP list however I have no idea where to start and would like to request some lights on how to do it.

Thank you all

What I have tried:

Im looking for some guidance about what can be done because I don't know what approaches can be taken
Posted
Updated 2-Oct-20 12:42pm
Comments
Richard Deeming 5-Oct-20 11:04am    

1 solution

How about start from below:
Python
import ipaddress
def process(iptext):
    try:
        # do your thing here
        # example
        print(ipaddress.ip_interface(iptext).network)
        return
    except Exception:
        print("INVALID")
        return 

with open('ipaddresses.txt', 'r') as f:
    for line in f:
        line = "".join(line.split())
        process(line)

Your ipaddresses.txt would have IPs like:
192.168.100.12/24
192.168.101.32/24

Looks nice reference related to IP: Working with IP addresses in Python - ipaddress library - part 1 |[^]
 
Share this answer
 
Comments
Member 14651994 2-Oct-20 20:22pm    
Thank you for your reply. Can you just tell me what is the "#do your thing here #example"?
Sandeep Mewara 3-Oct-20 3:31am    
Whatever *you* intend to do with the IP address.

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