From 2212cf24b4c1fa4dbda6c1e99ec3b33f53aece64 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 4 Nov 2014 00:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=A4=84=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/jumpserver.iml | 2 +- jumpserver.py | 7 ++- webroot/AutoSa/AutoSa/log_handler.py | 82 ++++++++++++++++++++++++++++ webroot/AutoSa/UserManage/models.py | 2 + 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 webroot/AutoSa/AutoSa/log_handler.py diff --git a/.idea/jumpserver.iml b/.idea/jumpserver.iml index 9dcd47829..94e54876c 100644 --- a/.idea/jumpserver.iml +++ b/.idea/jumpserver.iml @@ -2,7 +2,7 @@ - + diff --git a/jumpserver.py b/jumpserver.py index fcd58ae12..5b7efa806 100755 --- a/jumpserver.py +++ b/jumpserver.py @@ -5,7 +5,6 @@ import os import sys import subprocess -import MySQLdb import struct import fcntl import termios @@ -17,6 +16,7 @@ from binascii import b2a_hex, a2b_hex import ConfigParser import paramiko import pxssh +import pexpect cur_dir = os.path.dirname(__file__) sys.path.append('%s/webroot/AutoSa/' % cur_dir) @@ -131,7 +131,7 @@ def connect(host, port, user, password): foo.login(host, user, password, port=port, auto_prompt_reset=False) log = Logs(user=user, host=host, logfile=logfile_name, start_time=timestamp_start, ppid=os.getpid()) # 日志信息记录到数据库 log.save() - pid = Pid(ppid=os.getpid(), cpid=foo.pid) + pid = Pid(ppid=os.getpid(), cpid=foo.pid, start_time=timestamp_start, logid=log.id) pid.save() logfile = open(logfile_name, 'a') # 记录日志文件 @@ -145,8 +145,11 @@ def connect(host, port, user, password): log.finish = 1 log.end_time = int(time.time()) log.save() + except pxssh.ExceptionPxssh as e: print('登录失败: %s' % e) + except pexpect.EOF: + print('登录失败: Host refuse') except KeyboardInterrupt: foo.logout() log.finish = 1 diff --git a/webroot/AutoSa/AutoSa/log_handler.py b/webroot/AutoSa/AutoSa/log_handler.py new file mode 100644 index 000000000..e0e6c7192 --- /dev/null +++ b/webroot/AutoSa/AutoSa/log_handler.py @@ -0,0 +1,82 @@ +#!/usr/bin/python +#coding: utf-8 + +import os +import sys +import time + +cur_dir = os.path.dirname(__file__) +sys.path.append('%s/webroot/AutoSa/' % cur_dir) +os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings' + +from UserManage.models import Logs, Pid + + +def log_hanler(logid): + log = Logs.objects.filter(id=logid) + if log: + log = log[0] + filename = log.logfile + os.system('cat %s | grep "DateTime" > %s.his' % (filename, filename)) + os.system('cat %s | grep "\[.*@.*\][\$\#]" >> %s.his' % (filename, filename)) + os.system('cat %s | grep "EndTime" >> %s.his' % (filename, filename)) + + +def set_finish(id): + logs = Logs.objects.filter(id=id, finish=0) + if logs: + structtime_start = time.localtime() + timestamp_end = int(time.mktime(structtime_start)) + log = logs[0] + log.finish = 1 + log.end_time = timestamp_end + log.save() + + +def kill_pid(pid): + try: + os.kill(pid, 9) + except OSError: + pass + + +def pid_exist(pid): + pid_dir = "/proc/%s" % pid + if os.path.isdir(pid_dir): + return True + else: + return False + + +def get_pids(): + pids = [] + pids_obj = Pid.objects.all() + for pid_obj in pids_obj: + pids.append((pid_obj.ppid, pid_obj.cpid, pid_obj.logid, pid_obj.start_time)) + return pids + + +def run(): + for ppid, cpid, logid, start_time in get_pids(): + if pid_exist(cpid): + if pid_exist(ppid): + structtime_start = time.localtime() + timestamp_end = int(time.mktime(structtime_start)) + if timestamp_end - start_time > 7200: + kill_pid(ppid) + kill_pid(cpid) + set_finish(logid) + log_hanler(logid) + else: + kill_pid(cpid) + set_finish(logid) + log_hanler(logid) + else: + set_finish(logid) + log_hanler(logid) + + +if __name__ == '__main__': + while True: + run() + time.sleep(0.5) diff --git a/webroot/AutoSa/UserManage/models.py b/webroot/AutoSa/UserManage/models.py index a257e0f68..6ccbdcbdd 100644 --- a/webroot/AutoSa/UserManage/models.py +++ b/webroot/AutoSa/UserManage/models.py @@ -43,4 +43,6 @@ class Pid(models.Model): """ ppid = models.IntegerField() cpid = models.IntegerField() + logid = models.IntegerField() + start_time = models.IntegerField()