Python uses the Python-nmap module for port scanners

Source: Internet
Author: User
Tags server port

Port scanning on an online server can be useful to verify your firewall rules and avoid unwanted services. You can also know what services you have opened on your machine, do not have to wait for the clouds to burst out to know, someone hacked into the intranet for several months. Haha, the real thing, the server was passed Zabbix Black in, opened a socket5 process, free access. Anyway, this thing is very useful, in the spirit of dedication, put the code out and learn together.


The function is very simple, scan the server, generate the scan result in HTML format, send the message to the scan result. Format to do a bit of processing, define the port whitelist, normal port display green, abnormal port display red. Kind of an alarm. Full port scanning of the server is a time-consuming thing, with 60,000 + ports per port, and also depending on the network connectivity of the scanning machine to the target machine. Can not be affected by this snail speed, the development of the first version of the single-threaded version, and realized a multi-process version, really cool a lot. The whole person is all right ...


mytools.py This is the definition of a library of functions, the use of a function, the sendemail of the e-mail function, of course, the current scene can be defined in a file, but it is a good habit to split the program by module. Haha, I'm a little pythonic.

#-*- coding:utf-8 -*-import smtplibfrom email.mime.text import mimetextfrom  email.header import headerdef sendemail (Sender,receiver,subject,content,smtpserver, Smtpuser,smtppass):     msg = mimetext (content, ' html ', ' Utf-8 ') #中文需参数 ' Utf-8 ', Single-byte characters do not require     msg[' Subject '] = header (subject,  ' utf-8 ')      msg[' from '] =  ' <%s> '  % sender    msg[' to '] = ; '. Join (receiver)     try:        smtp =  Smtplib. SMTP ()         smtp.connect (smtpserver)          smtp.login (Smtpuser, smtppass)          Smtp.sendmail (Sender, receiver, msg.as_string ())          Smtp.quit ()     excePt exception,e:        print e 


nmscan.py implementation of the port scanning program, single-threaded version, the code is a bit long, cautious into

#!/usr/bin/python#-*- coding:utf-8 -*-import nmapimport reimport mytools as  toolimport sys  reload (SYS)   sys.setdefaultencoding (' UTF8 ') Def nmScan ( Hostlist,portrange,whitelist):         p = re.compile ("^ (\d*) \-(\d*) $ ") If type (hostlist)  != list:             help ()         portmatch = re.match (p, Portrange) if not portmatch:             Help ()         l = []         for host in hostlist:             result =  '     nm = nmap. Portscanner () &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;  tmp = nm.scan (Host,portrange)              result = result +  "&LT;H2&GT;IP address:%s  hostname: [%s]  .......   %s


mutinmscan.py port scan of the multi-process version, compared to single-threaded version of the largest one change is the implementation of the Nmscan function, single-threaded to pass a list of servers, inside the function loop the list, read the scan results, generate report messages. The function of version Mutinmscan is to accept an IP address, which uses the pool of the Mutiprocess library and uses its map function to iterate through the list of server IPs. Multi-threading, a section more than five knots strong ...

#!/usr/bin/python#-*- coding:utf-8 -*-import nmapimport reimport mytools as  toolimport sysfrom multiprocessing import Poolfrom functools import  Partialreload (SYS) sys.setdefaultencoding (' UTF8 ') Def nmscan (host,portrange,whitelist):         p = re.compile ("^ (\d*) \-(\d*) $")          # if type (hostlist)  != list:        #      help ()         portmatch =  Re.match (P,portrange)         if not portmatch:             help ()          if host ==  ' 121.42.32.172 ':             whitelist =&nbSp [25,]        result =  '          nm = nmap. Portscanner ()         tmp = nm.scan (host,portrange)          result = result +  "&LT;H2&GT;IP address:%s  hostname: [% S]  ......  %s


Scan results: Mosaic block The progress of human civilization, especially in the appreciation of the island action film, but, pro, I can not give our server to you to see, you understand!

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/A8/wKioL1TbB2fgJ6ttAASQJ0Evbg4920.jpg "title=" Scan.png "alt=" Wkiol1tbb2fgj6ttaasqj0evbg4920.jpg "/>

This article is from the "Candle Shadow Red" blog, be sure to keep this source http://gccmx.blog.51cto.com/479381/1613683

Python uses the Python-nmap module for port scanners

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.