Search code examples
pythonnetworkingnmapport-scanning

How to find out how many clients are on a certain address range?


I tried googling for this but i didnt find anything... I am building a port scanner and i would like to make it so, that i can scan a network range e.g 192.168.2.* and find out how many computers are on that range that are online. Alot like Nmap. I am programming in python. Is this possible in Python?


Solution

  • Here is Draft example that you can start with:

    import socket
    
    addr_range = "192.168.1.%d"
    
    ip_address_up = []
    
    # Use UDP. 
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    s.settimeout(2.0)
    
    for i in range(1, 254):
        try:
            ip = addr_range % i
            socket.gethostbyaddr(ip)
            ip_address_up.append(ip)
        except socket.herror as ex:
            pass
    
    print ip_address_up
    

    or something like this using ICMP (ping) rather thank UDP:

    import socket
    import ping
    
    ip_address_up = []
    
    addr_range = "192.168.1.%d"
    
    for i in range(1, 254):       
       try:
           ip = addr_range % i
           delay = ping.do_one(ip, timeout=2)
           ip_address_up.append(ip)
       except (socket.herror, socket.timeout) as ex:
           pass
    
    print ip_address_up