Skip to content

Commit

Permalink
自动扫描IP完成,接下来自动扫描主机信息
Browse files Browse the repository at this point in the history
  • Loading branch information
qingduyu committed Oct 12, 2018
1 parent 21df625 commit c77fdbb
Show file tree
Hide file tree
Showing 15 changed files with 1,295 additions and 494 deletions.
669 changes: 384 additions & 285 deletions .idea/workspace.xml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions apps/CMDB/serialize/ip_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from rest_framework import serializers
from CMDB.model.server_models import IpSource

class IP_Serializer(serializers.ModelSerializer):
class Meta:
model = IpSource
fields='__all__'
16 changes: 14 additions & 2 deletions apps/CMDB/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from CMDB.views.db.msyql_api import MysqlClusterAPI,MysqlDBAPI,MysqlInstanceAPI,MySQLUserAPI

from CMDB.views.server.scan_conf import scan_host_conf,scan_host_ip_add,scan_host_ip_del,scan_host_conf_upate
from CMDB.views.server.scan_ip import scan_ip,scan_ip_edit
from CMDB.views.server.scan_ip_api import IPAPI
from CMDB.views.server.scan_host import scan_host,scan_host_edit


urlpatterns = [
Expand Down Expand Up @@ -77,8 +80,17 @@
##############自动扫描相关#######
url(r'^scan/host/conf$',scan_host_conf,name='scan_host_conf'),
url(r'^scan/host/conf_upate/$',scan_host_conf_upate,name='scan_host_conf_update'),
url(r'^scan/host/ip_del/(?P<id>[0-9]+)$', scan_host_ip_del, name='scan_conf_ip_del'),
url(r'^scan/host/ip_add/$', scan_host_ip_add, name='scan_conf_ip_add'),
url(r'^scan/host/conf/ip_del/(?P<id>[0-9]+)$', scan_host_ip_del, name='scan_conf_ip_del'),
url(r'^scan/host/conf/ip_add/$', scan_host_ip_add, name='scan_conf_ip_add'),

url(r'^scan/ip/scan/$', scan_ip, name='cmdb_scan_ip'),
url(r'^scan/ip/edit/$', scan_ip_edit, name='cmdb_scan_ip_edit'),
url(r'ipresource/api/$',IPAPI.as_view(),name='cmdb_ip_api'),

url(r'^scan/host/scan/$', scan_host, name='cmdb_scan_host'),
url(r'^scan/host/edit/$', scan_host_edit, name='cmdb_scan_host_edit'),
url(r'host/api/$', IPAPI.as_view(), name='cmdb_host_api'),

##################
# url(r'^netasset/full$', netdevice.netasset, name='netasset_full'),
# url(r'^netasset/add/$', netdevice.asset_add, name='netasset_add'),
Expand Down
14 changes: 10 additions & 4 deletions apps/CMDB/views/server/scan_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ def scan_host_conf(request):

def scan_host_conf_upate(request):
if request.method == 'POST':
data=request.data

json_data = {'code': 500, 'msg': '数据添加失败,请检查数据格式'}
data=request.POST
Ports=scan_conf_port.objects.first()
SSH_pass=scan_conf_sshpass.objects.first()
Ports.port = data['ports']
SSH_pass.ssh_pass = data['ssh_pass']
Ports.save()
SSH_pass.save()

json_data = {'code': 200, 'msg': '配置完毕'}
return JsonResponse(json_data)


Expand All @@ -34,7 +40,7 @@ def scan_host_ip_add(request):
try:
scan_conf_ip.objects.create(nets=data['nets'],nets_pass=data['nets_pass'])
json_data = {'code': 200, 'msg': 'IP地址段添加成功'}
return JsonResponse(json_data)
return Response(json_data,content_type="application/json")
except Exception as e:
print(e)
json_data = {'code': 500, 'msg': 'IP地址段添加失败'+e}
Expand Down
233 changes: 168 additions & 65 deletions apps/CMDB/views/server/scan_host.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,176 @@
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# _#_ coding:utf-8 _*_

import os
from CMDB.views.server.scan_lib.nmap_all_server import snmp_begin
#扫描配置

from CMDB.views.server.scan_lib.postdata import post_data
from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from rest_framework.response import Response
from CMDB.model.server_models import Host
from CMDB.model.server_models import IpSource,scan_conf_ip,scan_conf_port,scan_conf_sshpass
from CMDB.views.server.tasks.do_scan_host import do_scan_host
from CMDB.views.server.tasks.deploy_key import deploy_key

import yaml
import json
# import sys


def scanhost():
def scan_host(request):
'''
主机扫描的界面展示
:return:
'''
















def main():
'''
开始扫描所有网段
:return:
'''
f=open('conf/scanhosts.yaml','r')
yamlfile=f.read()
s_conf = yaml.load(yamlfile)
print (type(s_conf))
s_nets = s_conf['hostsinfo']['nets']
s_netpass = s_conf['hostsinfo']['netpass']
s_ports = s_conf['hostsinfo']['ports']
s_pass = s_conf['hostsinfo']['ssh_pass']
s_cmds = s_conf['hostsinfo']['syscmd_list']
s_keys = s_conf['hostsinfo']['ssh_key_file']
s_blacks = s_conf['hostsinfo']['black_list']
server_ip = s_conf["server"]
token=s_conf['token']


'''
ËùÓÐÍø¶Î¿ªÊ¼É¨Ãè
'''
for nmap_type in s_nets:
allip_dict,host_faillogin_dict,host_login_dict = snmp_begin(nmap_type,s_netpass,s_ports,s_pass,s_keys,s_cmds,s_blacks)
allip_dict['token']=token
allip_info = json.dumps(allip_dict)
post_data("http://{0}/cmdb/scanip_api".format(server_ip), allip_info)
host_login_dict['token']=token
host_asset_info= json.dumps(host_login_dict)
post_data("http://{0}/cmdb/scanhost_api".format(server_ip), host_asset_info)
host_faillogin_dict['token']=token
host_fail_info= json.dumps(host_faillogin_dict)
post_data("http://{0}/cmdb/host_fail_api".format(server_ip), host_fail_info)

endtime = datetime.datetime.now()
totaltime = (endtime - starttime).seconds

logging.info("{Finish:Use time %s s}"%totaltime)
print "{Finish:Use time %s s}"%totaltime
if request.method=="GET": #默认查看
ip_duans= scan_conf_ip.objects.all()
ipduan=dict()
for i in ip_duans:
ipduan['id']=i.id
ipduan['name']=i.nets
return render(request, 'cmdb/servers/scan_host.html', locals())
if request.method=='POST':
'''进行后台扫描函数'''
ip_duans=scan_conf_ip.objects.all()
ports=scan_conf_port.objects.values('port').first()['port'].split(',')
ssh_pass=scan_conf_sshpass.objects.values('ssh_pass').first()['ssh_pass'].split(',')

for ip_duan in ip_duans:
nets_pass = ip_duan.nets_pass.split(',')
asset_info=do_scan_host(list(ports),ip_duan.nets,list(nets_pass),list(ssh_pass))
for item in asset_info:
print item
ip = item
ssh_port = asset_info[item]['ssh_port']
ssh_user = asset_info[item]['ssh_user']
ssh_passwd = asset_info[item]['ssh_passwd']
sn = asset_info[item]['sn']
system_ver = asset_info[item]['system_ver']
hostname = asset_info[item]['hostname']
ssh_status = asset_info[item]['ssh_status'][0]
ssh_rsa = asset_info[item]['ssh_rsa']
rsa_pass = asset_info[item]['rsa_pass']
vendor = asset_info[item]['machine_type']
cpu_model = asset_info[item]['cpu_model']
cpu_num = asset_info[item]['cpu_num']
memory = asset_info[item]['mem_total']
disk = asset_info[item]['disk']
status = '1'

try: # 如果主机存在,并且,扫描的IP 在IP 资源表中,则主机的IP_others 增加IP
# ipsource=IpSource.objects.get(ip=ip)
host = Host.objects.get(hostname=hostname)
host.hostname = hostname
host.system_type = system_ver
host.ssh_port = ssh_port
host.ssh_user = ssh_user
host.ssh_passwd = ssh_passwd
host.ssh_rsa = ssh_rsa
host.rsa_pass = rsa_pass
host.sn = sn
host.cpu_model = cpu_model
host.cpu_num = cpu_num
host.memory = memory
# host.disk=disk
host.status = '1'
host.ssh_status = ssh_status
host.vendor = vendor
if ip[0:3] == '172':
host.idc_id = '1'
elif ip[0:3] == '10.':
host.idc_id = '2'
if vendor[0:6] == 'XenHVM':
host.asset_type = '2'
ip_temp = host.ip_other.split(',')
tem = 0 # 如果 ip other 有这个ip 就是已经保存了这个IP,忽略,增加,如过没有则保存
for i in ip_temp:
if i == ip:
tem = 1
if tem == 0:
host.ip_other = ip + ',' + host.ip_other
host.save()
except Exception as msg: # 如果找不到这个主机就认为是新的,就去部署ansible
print(msg)
host = Host()
ssh_pwd = ssh_passwd
data = deploy_key(ip, int(ssh_port), ssh_pwd)
host.hostname = hostname
host.system_type = system_ver
host.ssh_port = ssh_port
host.ssh_user = ssh_user
host.ssh_passwd = ssh_passwd
host.ssh_rsa = ssh_rsa
host.rsa_pass = rsa_pass
host.sn = sn
host.cpu_model = cpu_model
host.cpu_num = cpu_num
host.memory = memory
host.status = '1'
host.ssh_status = ssh_status
host.vendor = vendor
host.ip = ip
host.ip_other = ip
if ip[0:3] == '172':
host.idc_id = '1'
elif ip[0:3] == '10.':
host.idc_id = '2'
if vendor[0:5] == 'XenHVM':
host.asset_type = '2'
host.save()

json_data = {'code': 200, 'msg': '主机扫描完成'}
return Response(json_data,content_type="application/json",status=200)







def scan_host_edit(request):
return True







#
#
# def main():
# '''
# 开始扫描所有网段
# :return:
# '''
# f=open('conf/scanhosts.yaml','r')
# yamlfile=f.read()
# s_conf = yaml.load(yamlfile)
# print (type(s_conf))
# s_nets = s_conf['hostsinfo']['nets']
# s_netpass = s_conf['hostsinfo']['netpass']
# s_ports = s_conf['hostsinfo']['ports']
# s_pass = s_conf['hostsinfo']['ssh_pass']
# s_cmds = s_conf['hostsinfo']['syscmd_list']
# s_keys = s_conf['hostsinfo']['ssh_key_file']
# s_blacks = s_conf['hostsinfo']['black_list']
# server_ip = s_conf["server"]
# token=s_conf['token']
#
#
# '''
# ËùÓÐÍø¶Î¿ªÊ¼É¨Ãè
# '''
# for nmap_type in s_nets:
# allip_dict,host_faillogin_dict,host_login_dict = snmp_begin(nmap_type,s_netpass,s_ports,s_pass,s_keys,s_cmds,s_blacks)
# allip_dict['token']=token
# allip_info = json.dumps(allip_dict)
# post_data("http://{0}/cmdb/scanip_api".format(server_ip), allip_info)
# host_login_dict['token']=token
# host_asset_info= json.dumps(host_login_dict)
# post_data("http://{0}/cmdb/scanhost_api".format(server_ip), host_asset_info)
# host_faillogin_dict['token']=token
# host_fail_info= json.dumps(host_faillogin_dict)
# post_data("http://{0}/cmdb/host_fail_api".format(server_ip), host_fail_info)
#
# endtime = datetime.datetime.now()
# totaltime = (endtime - starttime).seconds
#
# logging.info("{Finish:Use time %s s}"%totaltime)
# print "{Finish:Use time %s s}"%totaltime
42 changes: 42 additions & 0 deletions apps/CMDB/views/server/scan_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# _#_ coding:utf-8 _*_

#扫描配置

from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from rest_framework.response import Response

from CMDB.model.server_models import IpSource,scan_conf_ip
from CMDB.views.server.tasks.do_scan_ip import do_scan_ip
def scan_ip(request):
if request.method=="GET": #默认查看
ip_duans= scan_conf_ip.objects.all()
ipduan=dict()
for i in ip_duans:
ipduan['id']=i.id
ipduan['name']=i.nets
return render(request, 'cmdb/servers/scan_ip.html', locals())
if request.method=='POST':
'''进行后台扫描函数'''
ip_duans=scan_conf_ip.objects.all()
for ip_duan in ip_duans:
ips=do_scan_ip(ip_duan.nets)
for item in ips:
ip = item
try: # 如果主机存在,并且,扫描的IP 在IP 资源表中,则主机的IP_others 增加IP
ipsource = IpSource.objects.get(ip=ip)
except Exception as msg: # 如果找不到这个ip就认为是新的,就去部署ansible
ipsource = IpSource()
ipsource.ip = ip
ipsource.save()
json_data = {'code': 200, 'msg': 'ip扫描完成'}
return Response(json_data,content_type="application/json",status=200)





def scan_ip_edit(request):
return True
Loading

0 comments on commit c77fdbb

Please sign in to comment.