|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import socket |
| 5 | +import ipaddress |
| 6 | +import subprocess |
| 7 | +import threading |
| 8 | +import argparse |
| 9 | + |
| 10 | +# Create a thread class for simultaneous ping of all devices in the network range |
| 11 | +class MyThread(threading.Thread): |
| 12 | + def __init__(self, host): |
| 13 | + threading.Thread.__init__(self) |
| 14 | + self.host = host |
| 15 | + self.myres = None |
| 16 | + def run(self): |
| 17 | + # Call the ping process (count = 2, timeout = 1sec) |
| 18 | + if subprocess.call(['ping', '-c2', '-W1', str(self.host)], stdout=subprocess.DEVNULL) == 0: |
| 19 | + try: |
| 20 | + # Get the hostname of the device that replies to ping |
| 21 | + hostname = socket.gethostbyaddr(str(self.host))[0] |
| 22 | + except socket.herror: |
| 23 | + hostname = '**' |
| 24 | + self.myres = hostname |
| 25 | + |
| 26 | +# Thread list is created to store thread results and avoid simultaneous printing |
| 27 | +threads = [] |
| 28 | + |
| 29 | +def checkArgs(): |
| 30 | + # Parse the arguments to get the CIDR (CIDR addressing scheme, e.g. 192.168.1.0/24) |
| 31 | + parser = argparse.ArgumentParser(description='Network scan tool by Herl.') |
| 32 | + parser.add_argument("--cidr",default="192.168.1.0/24", help="e.g. 192.168.1.0/24") |
| 33 | + args=parser.parse_args() |
| 34 | + # check the validity of CIDR |
| 35 | + cidr = args.cidr.split("/") |
| 36 | + if len(cidr) != 2: |
| 37 | + print("Incorrect CIDR") |
| 38 | + exit() |
| 39 | + ip_addr = cidr[0].split(".") |
| 40 | + if len(ip_addr) != 4: |
| 41 | + print("Incorrect CIDR") |
| 42 | + exit() |
| 43 | + if ((not 0 <= int(ip_addr[0]) <= 255) \ |
| 44 | + or (not 0 <= int(ip_addr[1]) <= 255) \ |
| 45 | + or (not 0 <= int(ip_addr[2]) <= 255) \ |
| 46 | + or (not 0 <= int(ip_addr[3]) <= 255)): |
| 47 | + print("Incorrect CIDR") |
| 48 | + exit() |
| 49 | + if (not 0 <= int(cidr[1]) <= 32): |
| 50 | + print("Incorrect CIDR") |
| 51 | + exit() |
| 52 | + |
| 53 | + return args.cidr |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + try: |
| 57 | + # Check arguments given in parameters |
| 58 | + cidr = checkArgs() |
| 59 | + |
| 60 | + # Get network range as a list |
| 61 | + my_network = ipaddress.ip_network(cidr) |
| 62 | + # Loop on all devices of the network range |
| 63 | + for my_host in my_network.hosts(): |
| 64 | + # Create a thread for pinging the device and add the thread to the end of the thread list |
| 65 | + threads.append(MyThread(my_host)) |
| 66 | + # Start the previously created thread |
| 67 | + threads[-1].start() |
| 68 | + # Wait for all the threads to end and print results |
| 69 | + for thread in threads: |
| 70 | + # Wait the end of the run function |
| 71 | + thread.join() |
| 72 | + if thread.myres != None: |
| 73 | + print("host {} ({}) is up".format(thread.host, thread.myres)) |
| 74 | + except ValueError: |
| 75 | + print("ValueError exception raised") |
0 commit comments