Python 3 Multi Threaded Ping Sweep Scanner

We know that nmap and other tools are great for scanning a network. What if we want to do it in python?

The following code is a demonstration of multi threading, using python 3 to sweep a network using pings on windows. The single threaded version took almost 2 minutes to check a network, but this version takes about 2 seconds.

Why not NMAP for a Ping Sweep?

Arguably, nmap is better. But you may not have nmap installed, or you might want to learn about multithreading! Regardless, there are many things learned from writing scripts like this.

Python Style

There are things done in this script that might be written more elegantly, but for learning the basics of multi-threading, doing a ping sweep is a great example.

Topics Learned in This Script

Programming is all about the big picture. If you can imagine a concept, the rest is just pieces of a puzzle. It’s useful to break down tasks into smaller and smaller tasks and solve them 1 by 1. However, unless you have a big picture goal in mind, your code gets more like spaghetti as you add features and rewrite your dream into reality.

In this script, if you understand each line, you will also understand all of the following components:

  • importing modules in python
  • hiding windows using subprocess
  • calling a subprocess from OS using python
  • coloring the CLI of python output
  • reading CIDR network addresses using python
  • measuring python program run time
  • python functions
  • passing data into python functions
  • basic python variables
  • creating a list in python
  • accessing members of a list
  • if else logic and skipping branches in python
  • while and for loops
  • creating a thread queue in python
  • python worker pool creation
  • locking print threads while workers finish
  • converting to string
  • Converting a thread to daemon so it dies gracefully
  • printing without a newline
  • Finding length of a list
  • list iteration/looping over a list
  • formatting a float to precision desired

Mulithreaded Ping Sweeper in Python 3 Code

Leave a Reply

Your email address will not be published. Required fields are marked *