Beispiel #1
0
def snmp_begin(nmap_type, ports, password_list, i_key_file, syscmd_list,
               black_list, s_emails):
    '''
    执行系统主机扫描
    :param nmap_type:
    :param ports:
    :param password_list:
    :param i_key_file:
    :param i_key_file:
    :param syscmd_list:
    :param black_list:
    :param s_emails:
    :return:
    '''
    if nmap_type is None:
        return False

    nmap_net = '%s.0/24' % nmap_type
    #实例化扫描
    nm_item = NmapDev(black_list)
    #启动了SSH的设备IP和端口(sship_list),所有扫描到的主机IP(host_list),未启动SSH的设备(unkown__list)
    sship_list, host_list, unkown_list = nm_item.nmap_sship(ports, nmap_net)
    #可以登录SSH的设备IP,端口及登录信息等(canlogin_list);无法登录的SSH(notlogin_list)
    # 说明
    # canlogin_lst = {port, password,root,system_info, sys_hostname, sys_mac, machine_type}
    # canlogin_lst = {端口,密码,root,系统信息,系统名,mac地址,sn号,机器类型}
    canlogin_list, notlogin_list = nm_item.try_login(sship_list, password_list,
                                                     syscmd_list)
    #可以通过密钥登录的设备(key_login_list),无法通过密钥登录的设备(key_not_login_list)
    key_login_list, key_not_login_list = nm_item.try_key_login(
        notlogin_list, i_key_file, syscmd_list)

    print("Password Login ...", canlogin_list, notlogin_list)
    logger.info("Use password login:%s,%s" % (canlogin_list, notlogin_list))
    print("Key Login ...", key_login_list, key_not_login_list)
    logger.info("Use key login:%s,%s" % (key_login_list, key_not_login_list))

    email_message = u"(1)可以ssh 用户密码登录的服务器列表: \n %s \n \n (2)可以ssh 用户密钥登录的服务器列表 \n %s \n \n(3)无法ssh登录列表 \n %s \n \n (4)未知主机 \n %s" % (
        canlogin_list, key_login_list, notlogin_list, unkown_list)
    # email_message = u"发不出去我现在也很绝望啊"

    email_sub = u"系统扫描结果"
    receive_addr = s_emails
    email_s = sendmail(receive_addr, email_sub, email_message)
    x = email_s.send()
    print(x)

    HostLoginifo.objects.filter(ip__contains=nmap_type).delete()
    crpt_do = prpcrypt()

    if canlogin_list:
        for item in canlogin_list:
            mathine_t = canlogin_list[item][7] if canlogin_list[item][
                7] else u"未知(需要安装dmidecode工具)"
            crpt_pass = crpt_do.encrypt(
                canlogin_list[item]
                [1]) if canlogin_list[item][1] else crpt_do.encrypt('')
            HostLoginifo.objects.update_or_create(
                ip=item,
                ssh_port=str(canlogin_list[item][0]),
                ssh_passwd=crpt_pass,
                ssh_user=canlogin_list[item][2],
                ssh_status=1,
                ssh_type=0,
                system_ver=canlogin_list[item][3],
                hostname=canlogin_list[item][4],
                mac_address=canlogin_list[item][5],
                sn=canlogin_list[item][6],
                mathine_type=mathine_t)
    if key_login_list:
        for item in key_login_list:
            mathine_t = key_login_list[item][9] if key_login_list[item][
                9] else u"未知(需要安装dmidecode工具)"
            HostLoginifo.objects.update_or_create(
                ip=item,
                ssh_port=key_login_list[item][0],
                ssh_rsa=key_login_list[item][1],
                ssh_user=key_login_list[item][2],
                rsa_pass=key_login_list[item][3],
                ssh_status=1,
                ssh_type=key_login_list[item][4],
                system_ver=key_login_list[item][5],
                hostname=key_login_list[item][6],
                mac_address=key_login_list[item][7],
                sn=key_login_list[item][8],
                mathine_type=mathine_t)
    return unkown_list, key_not_login_list
def snmp_begin(nmap_type, ports, password_list, imoocc_key_file, syscmd_list,
               black_list, s_emails):
    '''
    执行系统主机扫描
    :param nmap_type:
    :param ports:
    :param password_list:
    :param imoocc_key_file:
    :param imoocc_key_file:
    :param syscmd_list:
    :param black_list:
    :param s_emails:
    :return:
    '''
    if nmap_type is None: return False

    nmap_net = '%s.0/24' % nmap_type
    nm_item = NmapDev(black_list)
    sship_list, host_list, unkown_list = nm_item.nmap_sship(ports, nmap_net)
    print(".................ssh ip list", sship_list)

    canlogin_list, notlogin_list = nm_item.try_login(sship_list, password_list,
                                                     syscmd_list)
    key_login_list, key_not_login_list = nm_item.try_key_login(
        notlogin_list, imoocc_key_file, syscmd_list)

    print("Password Login ...", canlogin_list, notlogin_list)
    logger.info("Use password login:%s,%s" % (canlogin_list, notlogin_list))
    print("Key Login ...", key_login_list, key_not_login_list)
    logger.info("Use key login:%s,%s" % (key_login_list, key_not_login_list))

    email_message = u"可以ssh 用户密码登录的服务器列表 \n %s \n 可以ssh 用户密钥登录的服务器列表 \n %s \n 无法ssh登录列表 \n %s \n 未知主机 \n %s"%(canlogin_list,\
                    key_login_list,notlogin_list,unkown_list)
    email_sub = u"系统扫描结果"
    receive_addr = s_emails
    email_s = sendmail(receive_addr, email_sub, email_message)
    email_s.send()

    HostLoginifo.objects.filter(ip__contains=nmap_type).delete()
    crpt_do = prpcrypt()

    if canlogin_list:
        for item in canlogin_list:
            mathine_t = canlogin_list[item][7] if canlogin_list[item][
                7] else u"未知(需要安装dmidecode工具)"
            crpt_pass = crpt_do.encrypt(
                canlogin_list[item]
                [1]) if canlogin_list[item][1] else crpt_do.encrypt('')
            HostLoginifo.objects.update_or_create(
                ip=item,
                ssh_port=str(canlogin_list[item][0]),
                ssh_passwd=crpt_pass,
                ssh_user=canlogin_list[item][2],
                ssh_status=1,
                ssh_type=0,
                system_ver=canlogin_list[item][3],
                hostname=canlogin_list[item][4],
                mac_address=canlogin_list[item][5],
                sn=canlogin_list[item][6],
                mathine_type=mathine_t)
    if key_login_list:
        for item in key_login_list:
            mathine_t = key_login_list[item][9] if key_login_list[item][
                9] else u"未知(需要安装dmidecode工具)"
            HostLoginifo.objects.update_or_create(
                ip=item,
                ssh_port=key_login_list[item][0],
                ssh_rsa=key_login_list[item][1],
                ssh_user=key_login_list[item][2],
                rsa_pass=key_login_list[item][3],
                ssh_status=1,
                ssh_type=key_login_list[item][4],
                system_ver=key_login_list[item][5],
                hostname=key_login_list[item][6],
                mac_address=key_login_list[item][7],
                sn=key_login_list[item][8],
                mathine_type=mathine_t)
    return unkown_list, key_not_login_list