Building a network scanner with Python

Python tools provide many native modules and support for working with sockets and TCP/IP in general. Additionally, Python can use the existing third-party commands available on the system to initiate the required scan and return the result. This can be done using the subprocess module that we discussed before, in Chapter 9, Using the Subprocess Module. A simple example is using Nmap to scan a subnet, as in the following code:

import subprocessfrom netaddr import IPNetworknetwork = "192.168.1.0/24"p = subprocess.Popen(["sudo", "nmap", "-sP", network], stdout=subprocess.PIPE)for line in p.stdout:    print(line)

In this example, we can see the following:

  • At the beginning, we imported the subprocess module ...

Get Hands-On Enterprise Automation with Python. now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.