The
Tag:smtplib exists login multipart lines formats stream att accessories
#!/usr/bin/env python3 #coding: Utf-8import timeimport commandsimport osimport smtplibimport email. Mimemultipartimport email. Mimetextimport email. Mimebaseimport mimetypesimport Email. Mimeimage#import mimeimage# function: Scan the host's port number 22nd is open and will be successful export log, if there is a port 22nd is detected public, then export the log, and immediately send mail to the responsible person Aochaunfei # The first step: Open the file (the file is stored in the private IP = public IP 22 format), the contents of the file a line of reading, while the Telnte operation # A. If Telnet returns a value of 0 indicating that Telnet is successful, the public network Port 22nd is exposed and there is a risk of being attacked. Will write to the due public IP in a file that begins with a time # B. Conversely, the public IP is written to a file in the beginning of another time as a distinction between f = open ("/lianxi/aochuanfei/telnet/telnet.txt", "R") E Xposeport_log = open ("/lianxi/aochuanfei/telnet/exposeport/exposeport.log", "w") Noexposeport_log = open ("/lianxi/ Aochuanfei/telnet/noexposeport/noexposeport.log "," W ") for Ip_port in F.readlines (): # There's a problem here, The Intercept is not # os.environ[' Ip_ Port '] = str (ip_port) #ip = str (ip_port.strip ()) IP = ip_port.split ("=") [ -1].strip () Print IP status = command S.getstatusoutput (' telnet $ip ') if status = = 0:exposeport_log.write (ip_port) Else:noexpoSeport_log.write (Ip_port) f.close () Exposeport_log.close () noexposeport_log.close () time = Time.strftime ('%Y-%m-%d%H :%m:%s ', Time.localtime (Time.time ())) Commands.getstatusoutput (' mv/lianxi/aochuanfei/telnet/exposeport/ exposeport.log/lianxi/aochuanfei/telnet/exposeport/$Time _exposeport.log ') commands.getstatusoutput (' mv/lianxi/ aochuanfei/telnet/noexposeport/noexposeport.log/lianxi/aochuanfei/telnet/noexposeport/$Time _noexposeport.log ') # Step Two: Send a file that exposes the public IP port number 22nd to a mail attachment as an email from the "[email protected]" to = "[email protected],[email Protected] "file_name ="/lianxi/aochuanfei/telnet/exposeport/$Time _exposeport.log "#附件名 server = Smtplib. SMTP ("smtp.163.com") Server.login ("Aochuanfei", "aixocm123") #仅smtp服务器需要验证时 # constructs the Mimemultipart object as the root container main_msg = email. Mimemultipart.mimemultipart () # Constructs a Mimetext object as a message display and attaches to the root container text_msg = email. Mimetext.mimetext ("Please note that the 22nd port of the public network in the attachment has been exposed", _charset= "Utf-8") Main_msg.attach (text_msg) # constructs the Mimebase object as the file attachment content and attaches to the root container ctype,encoding = Mimetypes.guess_type(file_name) If CType is None or encoding are not none:ctype= ' application/octet-stream ' maintype,subtype = ctype.split ('/' , 1) file_msg=email. Mimeimage.mimeimage (Open (file_name, ' RB '). Read (), subtype) print ctype,encoding## set attachment header basename = Os.path.basename ( file_name) File_msg.add_header (' content-disposition ', ' attachment ', filename = basename) #修改邮件头 Main_msg.attach (file_ msg) # Set root container Properties main_msg[' from '] = frommain_msg[' to '] = tomain_msg[' Subject '] = "PLEASE note!!!" [main_msg[' Date '] = email. Utils.formatdate () # Get formatted full text fulltext = main_msg.as_string () # Send mail with SMTP try:server.sendmail (from, To.split (', '), Fulltext) Finally:server.quit ()
Telnet scan of Port 22nd on public IP to see if there is an exposure problem and send mail in a timely manner