• python监控端口脚本[jkport2.0.py]


    #!/usr/bin/env python
    #!coding=utf-8
    import os
    import time
    import sys
    import smtplib
    from email.mime.text import MIMEText
    from email.MIMEMultipart import MIMEMultipart
    
    def sendsimplemail (warning):
        msg = MIMEText(warning)
        msg['Subject'] = 'python first mail'
        msg['From'] = 'root@localhost'
        try:
            smtp = smtplib.SMTP()
            smtp.connect(r'pop.qq.com')
            smtp.login('1427746783@qq.com', 'passwd')
            smtp.sendmail('1427746783@qq.com', ['1427746783@qq.com'], msg.as_string())
            smtp.close()
        except Exception, e:
            print e
    
    time_str =  time.strftime( "%Y-%m-%d", time.localtime( ) )
    file_name = "./" + time_str + ".log"
    
    while True:
        print_str = "";
        Pro_status = os.popen('netstat -tulnp | grep nginx','r').readlines()
        now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        try:
            if Pro_status == []:
                os.system('service nginx start')
                new_Pro_status = os.popen('netstat -tulnp | grep nginx','r').readlines()
                str1 = ''.join(new_Pro_status)
                port = str1.split()[3].split(':')[-1]
                if port != '9999':
                    print_str = print_str + "nginx start fail ..."
                else:
                    print_str = print_str + "nginx Program abort. an internal error has occurred" + '
    '
                    print_str = print_str + "nginx start success ..."
    #               sendsimplemail(warning = "This is a warning!!!")
            else:
                print_str = print_str + now_time +"nginx running ..."
            time.sleep(3)
            
            if os.path.exists ( file_name ) == False :
                os.mknod( file_name )
                handle = open ( file_name , "w" )
    #           print ( print_str + "**********1")
                try:
                 handle.write( print_str)
                except:
                 log.error('write backup error:')
                finally:
                 handle.close()
    #            print ( print_str + "**********2")
            else:
                handle = open ( file_name , "a" )
                print ( print_str + "**********3")
                handle.write( print_str + '
    ')
    
        except KeyboardInterrupt:
            sys.exit('
    ')
    

    这个是jkport更新版, 旧版在这里:python监控端口脚本

    新增日志输出功能

    运行模式, 直接守护:nohup ./jkport.py &

    不用像以前那么繁琐。

  • 相关阅读:
    2017年5月24日 HTML 基础知识(二)
    2017年5月22日 HTML基础知识(一)
    尼采语录
    Unicode字符串和整数和浮点数
    转义字符
    python第一节
    C# ASP .NET WEB方向和WPF方向,我该如何去选择
    ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL/SQL Statement ignored 问题
    C# WPF打印报表
    Sql Server 自定义数据类型
  • 原文地址:https://www.cnblogs.com/chenglee/p/7840146.html
Copyright © 2020-2023  润新知