MongoDB工作日常查询操作代码

自己把mondodb的增删更查操作封装在一个py文件中(mongo_operations.py)

# -*- coding:utf-8 -*-
import mongo_operations
import threading, re, datetime

mongo_data = mongo_operations.MdbOpera('*.*.*.*', *****, '******', '*********')
mongo_data.reconnect("scan_control")
with open('info.txt', 'r')as f:
    A = f.readlines()
def delay(A):
    B = []
    for i in A:
        print len(i)
        for ii in eval(i):
            B.append(str(ii).strip())
    return B


# print type(B)
def portscan(ip, time):
    A = []
    ret = mongo_data.findone({"ipv4": ip, "start_time": time}, collection="portscan")
    if ret:
        if ret['port_list']:
            for i in ret['port_list']:
                A.append(i['port'])
    return A


def nmapscan(ip, time):
    A = []
    ret = mongo_data.findone({"ipv4": ip}, collection="nmapscan")
    if ret:
        # print(ret['start_time'])
        if re.match(time, str(ret['start_time'])):
            print(ip)
            if len(ret['nmapscan_result']) > 0:
                for i in ret['nmapscan_result']:
                    if 'port_detail' in i and len(i['port_detail']) > 0:
                        for ii in i['port_detail']:
                            C = {}
                            C[ii['port']] = ii['service']
                            A.append(C)
    return A


def host_fingerprint(ip, time):
    A = {}
    ret = mongo_data.findone({"ipv4": ip}, collection="host_fingerprint")
    if ret:
        if re.match(time, str(ret['start_time'])):
            if ret['os']:
                A['os'] = ret['os']
            if len(ret['hostname']):
                A['hostname'] = ret['hostname']
    return A
#ipdomain
def ipdomain(ip, time):
    A = {}
    ret = mongo_data.findone({"ipv4": ip}, collection="ip_domainscan")
    if ret:
        if re.match(time, str(ret['start_time'])):
            if ret['domain_list']:
                A['domain_list'] = ret['domain_list']
            if len(ret['hostname']):
                A['hostname'] = ret['hostname']
    return A
#tcp
def tcpscan(ip, time):
    #A = {}
    ret = mongo_data.findone({"ipv4": ip}, collection="tcpscan")
    if ret:
        if re.match(time, str(ret['start_time'])):
            if ret['tcpscan_result']:
                ips = {}
                for i in ret['tcpscan_result']:
                    ips[i['port']] = i['service']
                    if i['connect_banner']!="":
                        if i['service']=="":
                            print(ip,i['connect_banner'])
                            #ip.append(i['connect_banner'])
                    else:
                        pass
                #A[ip] = ips
                return ips
                    #A['tcpscan_result'] = ret['tcpscan_result']
def main_port():
    res = {}
    B = delay(A)
    for i in B:
        
        C = nmapscan(str(i), '2019-04-18')
        #C=tcpscan(str(i),'2019-04-11')
        #E.append(C)
        # E.append(D)
        res[i] = C
    return res


def main_finger():
    res = {}
    B = delay(A)
    for i in B:
        E = []
        # C=nmapscan(str(i),'2019-04-11')
        D = host_fingerprint(str(i), '2019-04-11')
        # E.append(C)
        E.append(D)
        res[i] = E
    return res


if __name__ == '__main__':
    # delay(A)
    #with open('port.txt', 'w')as f:
    #    f.write(str(main_finger()))
    #with open('finger.txt', 'w')as f:
    #    f.write(str(main_finger()))
    print main_port()
 

你可能感兴趣的:(Python)