{"id":29,"date":"2023-12-04T10:41:13","date_gmt":"2023-12-04T10:41:13","guid":{"rendered":"https:\/\/networkbret.com\/?p=29"},"modified":"2023-12-04T10:41:13","modified_gmt":"2023-12-04T10:41:13","slug":"python-network-scanner-using-nmap","status":"publish","type":"post","link":"https:\/\/networkbret.com\/index.php\/2023\/12\/04\/python-network-scanner-using-nmap\/","title":{"rendered":"Python Network Scanner using Nmap"},"content":{"rendered":"\n<p>This is one of my first blog posts on this site so bear with me if it isn&#8217;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&#8217;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)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"260\" src=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/port-scanner-python.png\" alt=\"\" class=\"wp-image-30\" style=\"width:675px;height:auto\" srcset=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/port-scanner-python.png 468w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/port-scanner-python-300x167.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/figure>\n\n\n\n<p>Quick rundown of the code by line:<\/p>\n\n\n\n<p>1 import nmap imports the nmap module into python<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<p>lines 2-3 define different variables<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>line  5 defines port scanner object<\/p>\n\n\n\n<p>line 6 creates a a loop (including the +1 is for how python counts)<\/p>\n\n\n\n<p>line 7 creates a string variable<\/p>\n\n\n\n<p>line 8 overrides res variable and taking what we want from that scan.<\/p>\n\n\n\n<p>line 9 is a print function that politely lists the results of the scan for you. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next I go to command prompt and change the directory to that which my python is included with the cd command<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"76\" src=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/cd-command.png\" alt=\"\" class=\"wp-image-31\" srcset=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/cd-command.png 598w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/cd-command-300x38.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/figure>\n\n\n\n<p>After you are in the directory you want you can use the dir command to see what files are in that area <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"270\" src=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/dir.png\" alt=\"\" class=\"wp-image-32\" srcset=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/dir.png 850w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/dir-300x95.png 300w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/dir-768x244.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>Next I run the command with python main.py because I named the file &#8220;main.py&#8221;<\/p>\n\n\n\n<p>results: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"1024\" src=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/res-478x1024.png\" alt=\"\" class=\"wp-image-33\" srcset=\"https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/res-478x1024.png 478w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/res-140x300.png 140w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/res-768x1647.png 768w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/res-716x1536.png 716w, https:\/\/networkbret.com\/wp-content\/uploads\/2023\/12\/res.png 792w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><\/figure>\n\n\n\n<p>Okay you get the point I do not need to show you every port. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>One issue I had with this command was I kept getting the result &#8220;ModuleNotFoundError: no module named nmap&#8221;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>I found the fix to this was to go right into command line and use the pip command &#8220;pip install python-nmap&#8221;<\/p>\n\n\n\n<p>This worked immediately and after that the code ran very well. <\/p>\n\n\n\n<p>Click here to go to the github post about it: <a href=\"https:\/\/github.com\/NetworkBret\/nmap-python-scanner\">NetworkBret\/nmap-python-scanner (github.com)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is one of my first blog posts on this site so bear with me if it isn&#8217;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&#8217;s function throughout this post. (as best I can, I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/networkbret.com\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}