File size: 4,144 Bytes
08c964e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#coding: utf-8
import public,re,time,sys,os
from datetime import datetime


class safeBase:

    __isUfw = False
    __isFirewalld = False
    _months = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Sept':'09','Oct':'10','Nov':'11','Dec':'12'}

    def __init__(self):
        if os.path.exists('/usr/sbin/firewalld'): self.__isFirewalld = True
        if os.path.exists('/usr/sbin/ufw'): self.__isUfw = True

    #转换时间格式
    def to_date(self,date_str):
        tmp = re.split('\s+',date_str)
        if len(tmp) < 3: return date_str
        s_date = str(datetime.now().year) + '-' + self._months.get(tmp[0]) + '-' + tmp[1] + ' ' + tmp[2]
        time_array = time.strptime(s_date, "%Y-%m-%d %H:%M:%S")
        time_stamp = int(time.mktime(time_array))
        return time_stamp


    def to_date2(self,date_str):
        tmp = date_str.split()
        if len(tmp) < 4: return date_str
        s_date = str(tmp[-1]) + '-' + self._months.get(tmp[1],tmp[1]) + '-' + tmp[2] + ' ' + tmp[3]
        return s_date

    def to_date3(self,date_str):
        tmp = date_str.split()
        if len(tmp) < 4: return date_str
        s_date = str(datetime.now().year) + '-' + self._months.get(tmp[1],tmp[1]) + '-' + tmp[2] + ' ' + tmp[3]
        return s_date

    def to_date4(self,date_str):
        tmp = date_str.split()
        if len(tmp) < 3: return date_str
        s_date = str(datetime.now().year) + '-' + self._months.get(tmp[0],tmp[0]) + '-' + tmp[1] + ' ' + tmp[2]
        return s_date


    #取防火墙状态
    def CheckFirewallStatus(self):
        if self.__isUfw:
            res = public.ExecShell('ufw status verbose')[0]
            if res.find('inactive') != -1: return False
            return True

        if self.__isFirewalld:
            res = public.ExecShell("systemctl status firewalld")[0]
            if res.find('active (running)') != -1: return True
            if res.find('disabled') != -1: return False
            if res.find('inactive (dead)') != -1: return False
        else:
            res = public.ExecShell("/etc/init.d/iptables status")[0]
            if res.find('not running') != -1: return False
            return True
        return False


    def get_ssh_log_files(self,get):
        """
        获取ssh日志文件
        """
        s_key = 'secure'
        if not os.path.exists('/var/log/secure'):
            s_key = 'auth.log'
        if os.path.exists('/var/log/secure') and os.path.getsize('/var/log/secure') == 0:
            s_key = 'auth.log'

        res = []
        spath = '/var/log/'
        for fname in os.listdir(spath):
            fpath = '{}{}'.format(spath,fname)
            if fname.find(s_key) == -1 or fname == s_key:
                continue

            #debian解压日志
            if fname[-3:] in ['.gz','.xz']:
                if os.path.exists(fpath[:-3]):
                    continue
                public.ExecShell("gunzip -c " + fpath + " > " + fpath[:-3])
                res.append(fpath[:-3])
            else:
                res.append(fpath)

        res = sorted(res,reverse=True)
        res.insert(0,spath + s_key)
        return res
    
    def get_ssh_log_files_list(self,get):
        """
        获取ssh日志文件
        """
        s_key = 'secure'
        if not os.path.exists('/var/log/secure'):
            s_key = 'auth.log'
        if os.path.exists('/var/log/secure') and os.path.getsize('/var/log/secure') == 0:
            s_key = 'auth.log'

        res = []
        spath = '/var/log/'
        for fname in os.listdir(spath):
            fpath = '{}{}'.format(spath,fname)
            if fname.find(s_key) == -1 or fname == s_key:
                continue
            #debian解压日志
            if fname[-3:] in ['.gz','.xz']:
                continue
            if os.path.getsize(fpath) > 1024 * 1024 * 10:
                continue
            #判断文件数量为15个
            if len(res) > 15:
                break
            res.append(fpath)
        res = sorted(res,reverse=True)
        res.insert(0,spath + s_key)
        return res