Some time ago wrote a port scan of the Python script, found that now forget almost, and quickly recorded it, although it is concurrent, but in the actual use of the scene, if the scan of too many machines, there is still a performance problem, fortunately I need to scan the machine on a few. The script writes the raw ...
The following attached source:
#!/usr/bin/env python#-*- coding:utf-8 -*-import nmapimport threadingimport Smtplibimport stringfrom email.mime.text import mimetextfrom email.header import headerimport sys# Prevent Chinese problems reload (SYS) sys.setdefaultencoding (' Utf-8 ') in Linux #设置白名单接口PortList =[22,80,xxx , XXX] #设置收件人列表: Multiple needs to be separated by commas, such as: [' [' [email protected] ', ' [email protected] ', ' [email protected] ']maillist =[' [email protected] ', ' [email protected] ', ' [email protected] '] #定义个全局变量以接受scan1函数中的变量result = ' #总共执行的主机数, from the ip.txt to read the host, of course, some of the requirements may be from the database, where you can modify Num=file (' Ip.txt ', ' R ') Hostnum=len (Num.readlines ( )) Num.close () #定义发邮件函数def sendmail (sender,receiver,subject,content,smtpserver,smtpuser,smtppass): msg = mimetext (content, ' html ', ' Utf-8 ') 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# defining the sweep port function , the default port is 1-65535def scan1 (ip,port= ' 1-65535 '): nm = nmap. Portscanner () nm.scan (Ip,port) global result result = result + "
This article is from the "Smoke Free Hand" blog, please be sure to keep this source http://lidefu.blog.51cto.com/3429777/1719468
Python port scan