Working with Nmap through the os and subprocess modules
In this section, we will review how to execute nmap from the os
and subprocess
modules without needing to install any other dependency.
If you need to execute an nmap
command with the os
module, you don't need to install any additional dependencies and it's the easiest way to launch a nmap
command through the shell.
You can find the following code in the nmap_os.py
file:
import os nmap_command = "nmap -sT 127.0.0.1" os.system(nmap_command)
This could be the execution of the previous script where we are getting open ports on localhost:
$ sudo python3 nmap_os.py Nmap scan report for localhost (127.0.0.1) Host is up (0.000092s latency). Not shown: 998 closed ports PORT    STATE SERVICE 22/tcp  open  ssh 631/tcp open  ipp
Similarly, we could use the subprocess
module that provides the Popen
method for executing the nmap
command and pass the needed...