Python Implements port status scanning

Source: Internet
Author: User

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

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.