Brief introduction
Python-nmap is a Python library that uses nmap for port scanning, which makes it easy to generate NMAP scan reports and can help system administrators automate scan tasks and generate reports. At the same time, it also supports Nmap script output.
You can see that Python-nmap has only four py files (__init__.py, nmap.py, test.py, test_nmap.py), Here's a read.
__init__.py
Except for dozens of lines of comments, the real code is only four lines, the basic content is to import some basic information from the same directory nmap.py: Author (__author__), version (__VERSION__), last Modified date (__last_modification__), These are written under the nmap.py.
test.py
test.py also only a few simple lines, the runtime is to print out the local host, guessing is used to test whether you can use Nmap's scanning function
nmap.py
nmap.py is used to scan the function of Nmap, the main scanning function is Portscanner (object):
Class Portscanner (object):
The English notes of the Portscanner class are written to function: Portscanner class allows to using nmap from Python (Portscanner class allows Nmap to be used in Python)
def __init__ (Self, nmap_search_path= ('nmap'/usr/bin/nmap ') /usr/local/bin/nmap ' ' /sw/bin/nmap ' ' /opt/local/bin/nmap ')):
The main function is to find the path of Nmap in Nmap_search_path ( from Nmap_search_path you can see that you add a path under Windows ) and initialize Portscanner module, including the path of Nmap on this machine (Self._nmap_path), the results of the scan (Self._scan_result), the main version of Nmap (Self._nmap_version_number), Nmap Sub-version (Self._nmap_subversion_number), Nmap output version information (self._nmap_last_output), whether to find Nmap (is_nmap_found)
def get_nmap_last_output (self):
Returns the text output, which may be used for debugging, here is the author's English comment
def nmap_version (self):
If you check the version information that Nmap returns to Nmap
def Listscan (self, hosts='127.0.0.1'):
does not scan, but resolves the target host and returns a list of hosts
def scan (self, hosts='127.0.0.1', Ports=none, arguments='-sv ', Sudo=false):
Call Nmap scan function to scan, output in JSON format
def Analyse_nmap_xml_scan (self, nmap_xml_output=none, nmap_err=", nmap_err_keep_trace=" , nmap_warn_keep_trace='):
The scanning results of nmap are processed, and the scan results are in XML form, which is converted into JSON form to print.
def __getitem__ (Self, host):
Return Destination IP
def all_hosts (self):
Return destination IP as a list
def command_line (self):
Returns the input command line
def scaninfo (self):
Returns the scan information as a structural body
def scanstats (self):
Returns the scan status as a structural body
def has_host (self, host):
Returns true if the target host has a response (check if there is a target host)
def csv (self):
Convert CSV output to text return
def __scan_progressive__ (self, hosts, ports, arguments, Callback, sudo):
Callbacks for Portscannerasync
Class Portscannerasync (object):
Allow asynchronous use of nmap in Python, each host's scan results are returned through callbacks for multi-threaded scanning
def __init__ (self):
Call Portscanner () to check the system and the Nmap version of Nmap
def __del__ (self):
For self._process zeroing, self._process used to store scanning information
def scan (self, hosts='127.0.0.1', Ports=none, arguments='-sv ', Callback=none, Sudo=false):
A multithreaded scan is called, and the result is returned by a callback function
def Stop (self):
Stop the current scanning process
def Wait (self, timeout=none):
Waits for the end and timeout of the current scan process
def still_scanning (self):
Checks if the current process is still scanning
Class Portscanneryield (Portscannerasync):
Yield is called for processing by the scan result of the host
def __init__ (self):
Call Portscanner () to check the system and the Nmap version of Nmap
def scan (self, hosts='127.0.0.1', Ports=none, arguments='-sv ', Sudo=false):
Put the scan results in an iterator for callbacks
Class Portscannerhostdict (Dict):
Portscannerhostdict: Dictionary class for storing and accessing host scan results
def hostnames (self):
Return host names as a list
def hostname (self):
Returns the first hostname, not very understanding for compatibility issues
def State (self): def uptime (self):
two are all returned to the host state information
def all_protocols (self):
All_protocols: Returning a scanned protocol as a list
def all_tcp (self): def has_tcp (self, port): def TCP (Self, port):
the function of three functions is to list the information of the scanned TCP port
def all_udp (self): def has_udp (self, port): def UDP (Self, port):
the function of three functions is to list the information of the UDP port that is scanned
def all_ip (self): def has_ip (self, port): def IP (self, port):
The function of three functions is to list the scanned IP port information
def ALL_SCTP (self): def HAS_SCTP (self, port): def SCTP (self, port):
the function of three functions is to list the information of the scanned SCTP port.
Class Portscannererror (Exception):
Classes that detect exceptions for Portscanner
Return exception information
def __get_last_online_version ():
Get the latest Python-nmap version information, such as 0.6.1, by checking the website
def convert_nmap_output_to_encoding (value, code= "ASCII"):
Converts the encoding of a Unicode-encoded Scan_result object as a dictionary return
test_nmap.py
test_nmap.py is used to test python-nmap and to see if Nmap is working properly, whether the PDB library or the nose test framework is often used for Python testing and debugging
Class Pdb (Plugin):
Provides debugging options if an error or failure is encountered during a program test and is placed in the PDB for debugging
def options (self, parser, env):
defining command-line options: including--pdb,--pdb-failures,--pdb-errors
def Configure (Self, Options, conf):
to match which type of exception trigger plug-in by checking for exceptions
def adderror (self, Test, err):
If you configure debug results to debug errors put errors into the PDB
def addfailure (self, Test, err):
If the configuration debug result is debug failure put the error into the PDB
def debug (Self, err):
not very understanding, guess is output error message
def Setup_module ():
setting up a scan module for scanning
def Test_wrong_args ():
test the input error parameters
def Test_host_scan_error ():
test host scans for errors
def Xmlfile_read_setup ():
Test whether the XML file can be read
def Test_command_line ():
test whether the command line's commands are legitimate
def Test_scan_info ():
test whether the scan information exists
def test_all_hosts (): def test_host (): def test_host_no_hostname (): def Test_port ():
45.33.32.156 is a robot on Nmap's official website to test if Nmap works correctly.
def Test_listscan ():
Test List Scan Host results
def test_csv_output ():
test whether the output format is CSV
def Test_listscan ():
Test List Scan
def Test_ipv6 ():
test the scan of the IPV6
def Test_ipv4_async ():
testing asynchronous scans of IPV4
def Test_ipv6_async ():
testing asynchronous scans of IPV6
def Scan_localhost_sudo_arg_o ():
Scan Local Host users for information
def Test_sudo ():
Test host Information
def test_parsing_osmap_osclass_and_others ():
testing whether to get information about the host
def Test_all_protocols ():
test the local host for information
def Xmlfile_read_setup_multiple_osmatch ():
read Osmatch_output.xml file, set osmatch XML format output
def Test_multipe_osmatch ():
Check the host for any information in the Osmatch
def test_convert_nmap_output_to_encoding ():
test whether the NMAP scan results are encoded
def test_warning_case_sensitive ():
Test if the warning warning message exists
def test_scan_progressive ():
to test the condition of an asynchronous scan
Summarize
In General, is to write a Python script to invoke the functionality of the Nmap tool, but the idea is still worth our understanding and learning, the content of the possible analysis is not very comprehensive and there are some problems, there are questions can communicate with me, finally give me in the source of some notes and notes, Together with a blog to see.
Https://github.com/QKSword/python-nmap-analyse
function Learning of Python-nmap