Python_clamad implement efficient Port scanner Pythonnmappython third-party module PYTHONNMAP for efficient port scanning the installation method of the Python-nmap module is as follows: Yum -y install nmap #安装nmap工具pip install python-nmap# Module Source installation ## https://pypi.python.org/pypi/ python-nmaptar -zxvf python-nmap-0.1.4.tar.gzcd python-nmap-0.1.4python setup.py Installlinux under the Security Scanning Tool Nmap usage detailed http://www.aiezu.com/system/linux/linux_nmap_tutorial.html4.2.1 Module Common Method Description This section describes the Python-nmap module two common classes, one for the Portscanner () class, to implement a NMAP tool port scanning function encapsulation; the other is the Portscannerhostdict () class, To implement scan results for storage and access hosts, here are some common methods of the Portscanner () class. Scan (self,hosts= ' 127.0.0.1 ', ports=none,arguments= '-sv ') method, implements the specified host, port, A scan of nmap command line parameters. The parameter hosts are string types that represent the scanned host address, which can be expressed as "scanme.nmap.org", "198.116.0-255.1-127", "216.163.128.20/20", and the parameter ports as a string type. Represents the scanned port, which can be represented by "22,53,110,143-564", the parameter arguments as a string type, and the Nmap command line parameter, in the form "-SU-SX-SC", for example: Nm = nmap. Portscanner () Nm.scan (' 192.168.1.21-22 ', ' 22,80 ') command_line (self) method, the returned scan method maps to the specific NMAP command line, such as:>>> Nm.command_line () u ' nmap -ox - -p 22The 80 -sv 192.168.1.21-22 ' scaninfo (self) method returns NMAP scan information in the form of a dictionary type, such as: >>> nm.scaninfo () {u ' TCP ': {' services ': u ' 22,80 ', ' method ': u ' syn '}} all_hosts (self) To return the list of hosts scanned by Nmap, in the form of a list type such as: [U ' 192.168.1.21 ', u ' 192.168.1.22 '] The following describes some common methods of the Portscannerhostdict () class. The hostname (self) method, which returns the host name of the scanned object, such as:>> > nm[' 192.168.1.22 '].hostname () u ' sn2013-08-022 ' State (self) method, which returns the state of the scanned object, including 4 states (up, down, unknown, skipped ), such as: >>> nm[' 192.168.1.22 '].state () u ' Up ' all_protocols (self) method, returns the scanned protocol, such as: >>> nm[' The 192.168.1.22 '].all_protocols () [u ' TCP '] all_tcp () (self) method returns the port scanned by the TCP protocol, such as: >>> nm[' 192.168.1.22 ']. All_tcp () [22, 80] TCP (Self,port) method, which returns information about the scan TCP protocol port (port), such as: >>> nm[' 192.168.1.22 '].tcp (22) {' State ': u ' open ', ' reason ': u ' syn-ack ', ' name ': u ' ssh '}
#!/usr/bin/env python#_*_coding:utf-8 _*___author__ = ' Gaogd ' import sysimport Nmapscan_row=[]input_data = raw_input ("please input hosts and port: ") scan_ Row = input_data.split (" ") If len (scan_row)!=2: print "input errors,example \ "192.168.1.0/24 80,443,22\" " sys.exit (0) hosts= scan_row[0] #接收用户输入的主机port =scan_row[1] #接收用户输入的端口print hosts , ' : ' , porttry: 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: #调用扫描方法, parameters specify scan host hosts,nmap scan command line parameters Arguments nm.scan (hosts=hosts, arguments= ' -v -sS -p ' +port) except exception as 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, down for Proto in nm[host].all_protocols (): #遍历扫描协议, such as TCP, udp print ('----------') print (' protocol : %s ' % proto) #输入协议名 lport = nm[host][proto]. Keys () #获取协议的所有扫Stroke Port lport.sort () #端口列表排序 for port in lport: #遍历端口及输出端口与状态 print (' port : %s\tstate : %s ' % (port, nm[host][proto][port][' state '))
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1872995
Python third-party module PYTHONNMAP for efficient port scanning