ka.py

来源:互联网 发布:帝国cms 文字水印 编辑:程序博客网 时间:2024/05/01 11:29
#!/usr/bin/python3
#jason.ge 10th of May 2017
#version 0.a

#py read a file in kasperskyanalyze folder
import sys

CLEANMARKS = ['Backup Copy of Object Created','Disinfected On Restart','Object Blocked','Object Deleted','Object Disinfected','Object Moved To Quarantine']

#def linecheck():

def kascheck(data):
    cleanlog = []
    uncleanlog = []
    currentobj = '###'
    cleanedflag = False
    count = 0
    lastline = 'THIS IS FIRST LINE'
    for line in data:
        #print('[NO. %d] [current obj = %s] [last obj] = %s'%(count,currentobj,lastline)) #trouble shoot only
        count += 1
        if not (currentobj in line[0]):    #THIS IS NEW OBJ ANJD LAST OBJ HAS FINISHED WE NEED TO SEE IF IT IS CLEANED OR NOT
            #print('%s not in %s'%(currentobj,line[0]))    #for trouble shoot
            if (not cleanedflag) and (currentobj != '###'):    #IF LAST OBJ (NOW STILL IN CURRENT OBJ) IS NOT CLEANED
                uncleanlog.append('[UNCLEANED] %s %s'%(currentobj,lastline[3]))
            cleanedflag = False    #RESET CLEANEDFLAG TO FALSE
            currentobj = getobject(line[0])
        if not cleanedflag:
            for mark in CLEANMARKS:
                if (mark in line[1] or mark in line[2]):
                    cleanedflag = True
                    cleanlog.append('[CLEANED] %s CLEANED %s %s'%(line[0],line[1],line[2]))
        lastline = line
    return cleanlog,uncleanlog

def frominput():
    rawdata = []
    while True:
        inputcache = input()
        if inputcache == 'end':
            return rawdata
        elif inputcache:
            rawdata.append(inputcache)

def readfile(arg):#read certain file into a 2D list.
    data = []
    if 'i' in arg:
        tmp = frominput()
    else:
        fh = open('./kasperskyanalyze/data.csv')
        tmp = fh.readlines()
        fh.close()
    for i in tmp[1:]:
        #linecheck()
        i = i.split(',')
        data.append(i)
    for i in range(len(data)):
            data[i][0] = data[i][0].lower()
    return(data)

def getobject(line):
    p = line.find('/')
    if p != -1:
        return(line[:p])
    else:
        return(line)
        
def main():
    if len(sys.argv) == 1 or not ('u' or 'a' in sys.argv[1]):
        print('-u\tprint uncleaned only')
        print('-a\tprint all records')
        print('-i\tget data from input')
        sys.exit()
    try:
        data = readfile(sys.argv[1])
    except Exception as err:
        print('Error during input')
        print(err)
        sys.exit()
    
    try:
        cleanlog,uncleanlog = kascheck(data)
    except Exception as err:
        pass

    print('===     RESULTS ===')

    if 'u' in sys.argv[1]:
        for i in uncleanlog:
            print(i)
    elif 'a' in sys.argv[1]:
        for i in cleanlog:
            print(i)
        for i in uncleanlog:
            print(i)
main()

原创粉丝点击