zoukankan      html  css  js  c++  java
  • python:端口扫描邮件推送

    #!/usr/bin/env python
    import pickle
    import smtplib
    from email.mime.text import MIMEText
    import nmap
    from multiprocessing import Pool
    import time
    import os
    
    now = time.strftime("%Y%m%d")
    os.system('touch /tmp/%s'%now)
    def run(ip):
    	f = open('/tmp/%s'%now,'a')
    	nm = nmap.PortScanner()
    	nm.scan(ip)
    	for proto in nm[ip].all_protocols():
    		ports = nm[ip][proto].keys()
    		ports.sort()
    		port_length = len(ports)
    		i = 0
    		while i < port_length:
    			in_port = ports[i]
    			if nm[ip][proto][in_port]['state'] == 'open':
    				name = nm[ip][proto][in_port]['name']
    				f.write('%s %s-%d open => %s
    ' %(ip,proto,in_port,name))	
    				f.flush()
    			i += 1
    	f.close()
    
    def sendmail():
    	mail_host = 'smtp.intellicredit.cn'
    	mail_user = 'security@intellicredit.cn'
    	mail_pass = 'yourpassword'
    	sender = 'security@intellicredit.cn'
    	receivers = ['zhangshun@intellicredit.cn']
    	context = os.popen('cat /tmp/%s'%now).read()
    	message = MIMEText(context,'plain','utf-8')
    	message['Subject'] = 'boxing-Intranet-port-scan'
    	message['From'] = sender
    	message['To'] = receivers[0]
    	
    	try:
    	        smtpObj = smtplib.SMTP()
    	        smtpObj.connect(mail_host,25)
    	        smtpObj.login(mail_user,mail_pass)
    	        smtpObj.sendmail(sender,receivers,message.as_string())
    	        smtpObj.quit()
    	        print('success')
    	except:
    	        print('failed')
    
    if __name__ == '__main__':
    	iplist = pickle.load(open('/root/host.ini','rb'))
    #	iplist = ['10.0.17.20']
    	for ip in iplist:run(ip)
    	sendmail()
  • 相关阅读:
    批量修改mp3文件播放速度
    尝试了一下alsa录音播放的编程。
    最快速获取天气的方法
    乒乓Buffer
    GCC编译选项和环境变量
    Linux发行版本
    memcpy 和 memmove
    自动定位android ndk的路径
    记一下,安装和使用Ubuntu过程中遇到的问题
    Android API 和Android 版本对照
  • 原文地址:https://www.cnblogs.com/ywxbbbbb/p/10101123.html
Copyright © 2011-2022 走看看