切换风格

Wizard Sky California Sunset glow Black Cloud Beige Dragon Lavender NewYear City Snow Flowers London
收藏本站XSS平台字符串转换jsfuck
一个定时器定时去检查当前全部受控机器的监听状态[复制链接]
发表于 2013-10-21 18:03:42 | 显示全部楼层 |!read_mode!
#-*-coding:utf8-*-

"""

@copyright: V2010-11-10

@see: 一个定时器定时去检查当前全部受控机器的监听状态

"""

import pickle,string

from threading import Thread

import MySQLdb

import time,os,httplib,json,datetime,sys

import os

import socket, sys

logpath = "/tmp/telnet.log"

class ExtTimer(Thread):

    def __init__(self, interval, maxtime, func):

        self.__interval = interval

        self.__maxtime = maxtime

        self.__tottime = 0.0

        self.__func = func

        self.__flag = 0

        Thread.__init__(self, None, "ExtTimer", None)

    def run(self):

        while self.__flag == 0:

            time.sleep(self.__interval)

            conn = MySQLdb.connect("127.0.0.1","xxx","xxx","xxx",port=3306,connect_timeout=10,compress=True)

            cursor=conn.cursor()

            self.__func(cursor) 

            cursor.close()

            conn.close()

            if self.__maxtime > 0.0:

                self.__tottime += self.__interval

                if self.__tottime >= self.__maxtime:

                    self.end()

    def end(self):

        self.__flag = 1



class ExtTimerDummy:

    def __init__(self):

        self.__interval = 60.0

    def __timerFunc(self,cursor):

        parsedata(cursor)

    def createTimer(self, interval):

        self.__interval = interval

        self.__timer = ExtTimer(self.__interval, -20, self.__timerFunc)

        self.__timer.start()

def log(_msg):

    now = time.time()

    strtime = str(time.localtime(now)[0]) + '-' + str(time.localtime(now)[1]) \

                   + '-' + str(time.localtime(now)[2]) \

                   + ' ' + str(time.localtime(now)[3]) \

                   + ':' + str(time.localtime(now)[4]) \

                   + ':' + str(time.localtime(now)[5]);

    msg = strtime + ' ' + _msg + '\n';

    try:

        logfilehandle = open(logpath,'a+');

        logfilehandle.write(msg);#write file

        logfilehandle.close();

    except:pass;



    



def parsedata(cursor):

    try:

        #遍历hosts文件逐行检查其主机状态是否正常 

        myfile = open("/etc/hosts")

        for line in myfile.readlines():

            s = line.split()

            host = s[1].strip()

            try:

                s=socket.socket()

                s.settimeout(5.0)

                s.connect((host,51234))

                sql = "update xxx set isping='1' where hostname = '%s'" % host

                cursor.execute(sql)

            except Exception,e:

                print host

                sql = "update xxx set isping='0' where hostname = '%s'" % host

                cursor.execute(sql)

    except Exception,e:print str(e)

time_object1 = ExtTimerDummy()

time_object1.createTimer(5)


操千曲而后晓声,观千剑而后识器。
发表于 2014-8-1 21:24:07 | 显示全部楼层

该死的禁止灌水

代码区

GMT+8, 2019-11-19 21:52

Powered by Discuz! X2

© 2001-2018 Comsenz Inc.

回顶部