Python Module Learning----nmap Module

Source: Internet
Author: User
Tags ack xsl

Installing the Nmap module:

Pip Install Python_nma


Nmap Module Description:

Python-nmap is a Python library that helps to use the Nmap port scanner. It allows easy manipulation of NMAP scan results and will be a perfect choice for tools and reports from system administrators who need to automate the scanning task. It also supports Nmap script output.


Nmap Module class:

Et:python's lightweight XML support

Portscanner: Port Scan

Portscannerasync: Port scan is asynchronous

Portscannererror:nmap Class of Exception errors

Portscannerhostdict: Special small class for storing and accessing host scan results

Portscanneryield: Using Nmap in Python through the generator

Process: Represents an activity that runs in a separate process, which is supposed to control the scanning process.

Convert_nmap_output_to_encoding: Encoding

Csv:csv Operation Module

Io:

Nmap:

Os

Re

Shlex:

Subprocess:

Sys


See the general meaning, we are here mainly with Portscanner, Portscannererror, portscannerhostdict these three classes


Portscanner: Normal port Scan class

Initialize the Portscanner class, first look at the module's __init__ constructor description.

The detection system and NMAP on the nmap version may cause portscannererror exceptions if Nmap is not found in the path

__init__ (Self, nmap_search_path= (' Nmap ', '/usr/bin/nmap ', '/usr/local/bin/nmap ', '/sw/bin/nmap ', '/opt/local/bin/ Nmap '))

If you call the Portscanner module exception, it may be that the Nmap_search_path default path does not find your installed Nmap executable file, you need to manually modify the default value of this parameter.


The methods in this class illustrate:

all_hosts (): Returns the sorted list of all hosts Analyse_nmap_xml_scan (Nmap_xml_output=none,  nmap_err= ",  nmap_err_keep_trace=",  nmap_warn_keep_trace= "): Analysis Nmap xml Scan output param  Nmap_xml_output: XML string to parse Command_line (): Returns the command line for scanning CSV (): Returns the CSV output as text get_nmap_last_output () : Returns the last text output of Nmap with original text Has_host (host): Returns TRUE if the host has a result, otherwise returns Falselistscan (hosts= ' 127.0.0.1 '): Do not scan, But explain the target host and return a list of hosts Nmap_version (): If it detects a return nmap version of scan (hosts= ' 127.0.0.1 ',  ports=none, arguments= '-sv ',  sudo=false): Scan the specified host if Nmap output is not XML, Portscannererror exception may be thrown by the hosts: IP or IP segment to be scanned ports: parameter string for scanned port Arguments:nmap '-su -sx - SC ' sudo: whether to use sudo to start nmapscaninfo (): Return scaninfo structure {' TCP ':  {' services ':  ',  ' method ':  ' Connect '}}scanstats (): Return scanstats structure {' uphosts ':  ' 3 ',  ' timestr ':  ' thu jun  3  21:45:07 2010 ',  ' downhosts ':  ' 253 ',  ' totalhosts ', ':  ' ',  ', ' 5.79 ' elapsed ' 


Example Demo:

>>> nm = nmap. Portscanner ()   #实例化普通端口扫描对象nm >>> nm.nmap_version ()   #看看当前系统nmap版本 (7, 70)  # The current version is Nmap7.7>>> date = nm.scan (' 127.0.0.1 ', ' $ ', '-SV ')   #进行扫描, will pop up the Nmap cmd window, Will wait for the scan to end and return the scan results in JSON format date{' nmap ':  {' command_line ':  ' nmap -ox - -p 22 - sv 127.0.0.1 ',  ' scaninfo ':  {' tcp ':  {' method ':  ' syn ',  ' services ':  ' 22 '},   ' scanstats ':  {' timestr ':  ' fri apr 13 10:43:29 2018 ',  ' elapsed ':  ' 8.71 ',  ' uphosts ':  ' 1 ',  ' downhosts ':  ' 0 ',  ' totalhosts ':  ' 1 '}},  ' scan ':  {' 127.0.0.1 ':  {' hostnames ':  [{' name ':  ' ',  ' type ':  ' '}],  ' addresses ':  {' IPv4 ':  ' 127.0.0.1 '},  ' Vendor ': {},  ' status ':  {' state ':  ' up ',  ' reason ':   ' localhost-response '},  ' tcp ':  {22: {' state ':  ' open ',  ' reason ': ' Syn-ack ',  ' name ':  ' ssh ',  ' product ':  ' microsoft windows iot sshd ',  ' Version ':  ' 1.100 ',  ' extrainfo ':  ' protocol 2.0 ',  ' conf ':  ' to ',  ' of ' the CPE ':  ' Cpe:/o:microsoft:windows_10:::iot_core '}}}}}>>> nm.all_hosts () [' 127.0.0.1 ']>>>  Nm.command_line () ' nmap -ox - -p 22 -sv 127.0.0.1 ' >>> nm.csv () ' Host ; hostname;hostname_type;protocol;port;name;state;product;extrainfo;reason;version;conf;cpe\r\n127.0.0.1;;; Tcp;22;ssh;open; Microsoft windows iot sshd;protocol 2.0;syn-ack;1.100;10;cpe:/o:microsoft:windows_10:::iot _core\r\n ' >>> nm.get_nmap_last_output () ' <?xml version= "1.0"  encoding= "UTF-8"? >\r \n<! Doctype nmaprun>\r\n<?xml-stylesheet href= "file:///d:/program files  (x86)/Nmap/ Nmap.xsl " type=" text/xsl "?>\r\n<!-- nmap 7.70 scan initiated fri apr& Nbsp;13 10:43:20 2018 as: nmap -ox - -p 22 -sv 127.0.0.1  -->\r\n<nmaprun scanner= "Nmap"  args= "nmap -ox - -p 22 -sv  127.0.0.1 " start=" 1523587400 " startstr=" fri apr 13 10:43:20 2018 " version=" 7.70 " xmloutputversion=" 1.04 ">\r\n<scaninfo type=" SYN " protocol=" TCP " numservices=" 1 " services=" "/>\r\n<verbose level=" 0 "/>\r\n<debugging level=" 0 "/>\r\n


Portscannerhostdict Class Description:

The methods in this class illustrate:

ALL_IP (): Returns the IP Port list all_protocols (): Returns a list of all scan protocols >>> nm[' 127.0.0.1 '].all_protocols () [' TCP ']ALL_SCTP () : Return SCTP Port List all_tcp (): Return TCP port list >>> nm[' 127.0.0.1 '].all_tcp () [22]all_udp (): Return UDP port list has_ip (port): Returns true if the IP port has info, otherwise returns FALSEHAS_SCTP (port): True if the SCTP port has info, otherwise falsehas_tcp (port): Returns True if the TCP port has info Otherwise return FALSEHAS_UDP (port): Returns TRUE if the UDP port has info, otherwise returns FALSEHOSTNAME (): Returns the first hostname of a user record or List hostnames (): Returns the host list IP (port) : Return information for IP Port SCTP (port): Return information for SCTP Port State (): Return host status TCP (port): Return TCP port information >>> nm[' 127.0.0.1 '].state () ' Up ' UDP ( PORT): Returns the UDP port information uptime (): Returns the host status




Python Module Learning----nmap module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.