A Python namp module is required to implement port status scanning.
First to address: Https://pypi.python.org/pypi/python-nmap Download the Namp package.
I downloaded the version 0.6.1, the OS is CentOS 7.
Execute the following command to install the Namp module:
CD python-nmap-0.6.1
Python setup Install
The Python implementation code is as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#python Version:python 2.7.5
Import Sys
Import Nmap
Scan_row=[]
Input_data=raw_input ("Please input hosts and port:")
Scan_row=input_data.split ("")
If Len (scan_row)!=2:
print ' Input error! For example:192.168.18.128 80,443,22 '
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 ("Unexcepten error:", Sys.exc_info () [0])
Sys.exit (0)
Try
Nm.scan (hosts=hosts,arguments= '-v-ss-p ' +port)
Except Exception,e:
Print "Scan error:" +str (E)
For host in Nm.all_hosts ():
Print "--------------------------------------------"
Print (' Host:%s (%s) '% (host, Nm[host].hostname ()))
Print (' state:%s '% nm[host].state ())
For Proto in Nm[host].all_protocols ():
Print ('-------------')
Print (' Protocol:%s '% proto)
Lport=nm[host][proto].keys ()
Lport.sort ()
For Port in Lport:
Print (' Port:%s\tstate:%s '% (port,nm[host][proto][port][' state '))
Python Implements port status scanning