Python learning-python-nmap for efficient port scanners

Source: Internet
Author: User

Python's third-party module, PYTHON-NMAP for efficient port scanning.

First, the preparatory work

Yum-y Install nmap #安装nmap工具pip install python-nmap==0.4.0 #python2.7 Environment using PIP installation 0.4.0 version of the third-party module

Ii. description of common methods of modules

Here we mainly accept the Python-nmap module of the two common classes, one is the Portscanner () class, implementation of a NMAP tool port scanning function encapsulation, and the other for the Portscannerhostdict () class, to achieve storage and access to host scan results

1, Portscanner () class common methods

1-1. Scan () method
Scan (self, hosts= ' 127.0.0.1 ', Ports=none, arguments= '-SV ') method for scanning of specified host, port, and Namp command-line parameters. The parameter hosts is a string type, which indicates the host address of the scan, and the format can be expressed as "scanme.nmap.org", "192.116.0-255.1-127", "216.163.128.20/20"; The parameter ports is a string type that represents the scanned port, which can be represented by "22,53,110,143-4564", and the parameter Namp command-line arguments in the form "-SU-SX-SC", for example:

NM = Nmap. Portscanner () Nm.scan (' 192.168.209.121-122 ', ' 22,80 ')

1-2. Command_line () method
The Command_line (self) method, the returned scan method maps to the specific NMAP command line, such as:

>>> nm.command_line () u ' Nmap-ox-P 22,80-sv 192.168.209.121-122 '

1-3. Scaninfo () method
The Scaninfo (self) method returns NMAP scan information in the form of a dictionary type, such as:

>>>nm.scanninfo () {' TCP ': {' services ': ' 22,80 ', ' method ': ' Syn '}}

1-4. All_hosts () method
The All_hosts (self) method returns the list of hosts scanned by Nmap, in the form of a listing type, for example:

[' 192.168.209.121 ', ' 192.168.209.122 ']

2, Portscannerhostdict () class common methods

2-1. Hostname () method
The hostname (self) method, which returns the host name of the scanned object, such as:

>>> nm[' 192.168.209.121 '].hostname () ' Liuyazhuang '

2-2. State () method
The State (self) method, which returns the status of the scanned object, including the status of 4 (up, down, unknown, skipped), such as:

>>> nm[' 192.168.209.121 '].state () ' Up '

2-3. All_protocols () method
The All_protocols (self) method, which returns the scanned protocol, such as:

>>> nm[' 192.168.209.121 '].all_protocols () [' TCP ']

2-4. All_tcp () method
The All_tcp (self) method, which returns the port that the TCP protocol scans, such as:

>>> nm[' 192.168.209.121 '].all_tcp () [22,80]

2-5. TCP () method
The TCP (self, port) method, which returns information that scans the TCP protocol port (port), such as:

>>> nm[' 192.168.209.121 '].tcp (+) {' state ': ' Open ', ' reason ': ' Syn-ack ', ' name ': ' SSH '}

Third, code example

#!/usr/bin/python#coding=utf-8import sysimport nmap scan_row=[]input_data = raw_input (' please input hosts and port:  ')    #输入主机和端口scan_row  = input_ Data.split (" ")       #分割空格if  len (scan_row)!=2: #判断输入的字符长度不等于2      print  "input errors,example \" 192.168.1.0/24 80,443,22\ "   #输出   Input error     sys.exit (0) hosts=scan_row[0]     #接收用户输入的主机port =scan_row[1]      #接收用户输入的端口try:     nm = nmap. Portscanner ()      #创建端口扫描对象except  nmap. Portscannererror:    print (' Nmap not found ',  sys.exc_info () [0])      sys.exit (0) Except:    print ("Unexpected error:",  sys.exc_info () [0])     sys.exit (0) Try:    nm.scan (hosts=hosts, arguments='  -v -sS -p  ' +port)      #调用扫描方法, parameters specify scan host hosts, Nmap Scan command-line arguments argumentsexcept exception,e:    print  "Scan erro:" +str (e)      for host in nm.all_hosts ():     #遍历扫描主机      print ('----------------------------------------------------')     print (' host :  %s  (%s) '  %  (Host, nm[host].hostname ()))      #输出主机及主机名      print (' state : %s '  % nm[host].state ())      #输出主机状态 such as up and down     for proto in nm[host].all_protocols ():     #遍历扫描协议, such as TCP, Udp        print ('----------')          print (' protocol : %s '  % proto)      #输入协议名          lport&nbSp;= nm[host][proto].keys ()      #获取协议的所有扫描端口          lport.sort ()      #端口列表排序         for port  in lport:     #遍历端口及输出端口与状态              print (' port : %s\tstate : %s '  %  (port, nm[host][ proto][port][' state '))

The results are as follows:

Python learning-python-nmap for efficient port scanners

Related Article

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.