Python Port Scan Alarm

Source: Internet
Author: User

#!/usr/bin/python#coding=utf8# #import  sys,os,nmapimport multiprocessingimport httplib, Smtplibfrom email. Mimetext import mimetextfrom email. Header import headerreload (SYS) sys.setdefaultencoding (' UTF8 ') #设置收件人邮箱改成你自己的mailto_list =[' [email  protected] ']mail_host= "smtp.163.com"    #设置服务器mail_user = "[email protected]"       #用户名mail_pass = "password"     #密码mail_postfix = "163.com"    #发件箱的后缀def   Send_mail (to_list,sub,content):         me= "Server port Exception Alarm" + "<" +mail_user + "@" +mail_postfix+ ">"         msg = mimetext (content,_ Subtype= ' plain ', _charset= ' utf_8 ')         msg[' Subject '] =  sub        msg[' from '] = me         msg[' to '] =  ";". Join (to_list) &NBSP;&NBSP;&NBSP;&NBsp;    try:           server.login (Mail_user,mail_pass)            server.sendmail (Me,  to_list, msg.as_string ())             Server.close ()            return True         except Exception, e:            print str (e)             Return falsedef hostcheck (IPADDR):        nm =  Nmap. Portscanner ()         call = nm.scan (ipaddr,  ' 22-65535 ')  # scan host 127.0.0.1, ports from 22 to 443         Nm.command_line ()  # get command line used for the scan :  nmap -ox - -p 22-443 127.0.0.1         Nm.scaninfo ()  # get nmap scan informations {' TCP ':  {' services ':  ' 22-443 ',  ' method ':  ' Connect '}}        nm.all_hosts ()  #  get all hosts that were scanned         For host in nm.all_hosts ():                 for proto in nm[host].all_protocols ():                          pass                 lport = nm[host][protO].keys ()                  Lport.sort ()                  for port in lport:                         if port in white_port:                                  print line                          else:                                  line =  "HOST:&Nbsp; %s\ttcp/udp: %s\tport : %s\t   Abnormal Port " %  (host, proto,  Port)                                  f =file ('/tmp/ Problem_info.txt ', ' a ')                                   F.write ("\ r \ n")                                  f.write ( Line)                                  f.close ()  if  __name__ ==  "__maiN__ ":         input_ip = os.path.join (OS.GETCWD ()," IP.txt ")         input_ip_lines = sum (1 for line in  open (INPUT_IP))         open_input_ip = open (INPUT_IP)         if INPUT_IP_LINES > 30:                 process_number = 30         else:                  process_number = INPUT_IP_LINES          #设置白名单端口          white_port=[ 22,80,3306]        pool = multiprocessing. Pool (Processes=process_number)    &nbsP;    for ip in open_input_ip.readlines ():                 ip = ip.strip (' \ n ')                  pool.apply_async (HostCheck , (IP,))         pool.close ()          pool.join ()          #判断Problem_info文件是否存在          if os.path.exists ("/tmp/problem_info.txt"):                 infor=os.popen ("Cat /tmp/problem_info.txt"). Read ()                  #发送邮件报警                 send_mail ( Mailto_list, "admin", infor)                 os.system ("rm  -rf /tmp/problem_info.txt ")

Note: If the code is placed in the/root/directory, you need to create a IP.txt file in the/root/directory, and write the IP you need to scan.

This article is from the "Static Like Night Wind" blog, please make sure to keep this source http://siliotto.blog.51cto.com/8887165/1618757

Python Port Scan Alarm

Related Article

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.