This is one of my first blog posts on this site so bear with me if it isn’t the prettiest. I will be going over code I wrote in a way that may help you as well. This is the finished code, I will describe it’s function throughout this post. (as best I can, I am no expert for sure and may not use the best terms to describe what is going on)
Quick rundown of the code by line:
1 import nmap imports the nmap module into python
lines 2-3 define different variables
target on line 4 defines what IP address will be scanned, be careful with this as scanning an IP without permission can be illegal, I used the loopback address for this.
line 5 defines port scanner object
line 6 creates a a loop (including the +1 is for how python counts)
line 7 creates a string variable
line 8 overrides res variable and taking what we want from that scan.
line 9 is a print function that politely lists the results of the scan for you.
Next I go to command prompt and change the directory to that which my python is included with the cd command
After you are in the directory you want you can use the dir command to see what files are in that area
Next I run the command with python main.py because I named the file “main.py”
results:
Okay you get the point I do not need to show you every port.
One issue I had with this command was I kept getting the result “ModuleNotFoundError: no module named nmap”
I found the fix to this was to go right into command line and use the pip command “pip install python-nmap”
This worked immediately and after that the code ran very well.
Click here to go to the github post about it: NetworkBret/nmap-python-scanner (github.com)