项目地址:https://gitee.com/sdsnzy/cmdb
项目过程:
- Django自动化资产扫描----(1)项目简介并创建项目
- Django自动化资产扫描----(2)项目远程环境搭建
- Django自动化资产扫描(3)----存活探测(Nmap)、主机探测(Telnet)、ssh远程登陆(Paramiko)
八、定时扫描
8.1 Django数据库模型
1)编辑数据库模型
scanhost/models.py
from django.db import models
# Create your models here.
class Server(models.Model):
"""服务器设备"""
sub_asset_type_choice = (
(0, 'PC服务器'),
(1, '刀片机'),
(2, '小型机'),
)
created_by_choice = (
('auto', '自动添加'),
('manual', '手工录入'),
)
sub_asset_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name="服务器类型")
created_by = models.CharField(choices=created_by_choice, max_length=32, default='auto', verbose_name="添加方式")
IP = models.CharField('IP地址', max_length=30, default='')
MAC = models.CharField('Mac地址', max_length=200, default='')
model = models.CharField(max_length=128, null=True, blank=True, verbose_name='服务器型号')
hostname = models.CharField(max_length=128, null=True, blank=True, verbose_name="主机名")
os_type = models.CharField('操作系统类型', max_length=64, blank=True, null=True)
os_distribution = models.CharField('发行商', max_length=64, blank=True, null=True)
os_release = models.CharField('操作系统版本', max_length=64, blank=True, null=True)
def __str__(self):
return '%s-%s' % (self.id, self.hostname)
class Meta:
verbose_name = '服务器'
verbose_name_plural = "服务器"
2)创建数据库表及超级用户
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
8.2 配置文件
cmdb/settings/base.py
scanhosts = [
# '127.0.0.1',
# '172.25.254.0/24',
'192.168.43.0/24']
commands = {
'hostname': 'hostname',
'os_type': 'uname',
'os_distribution': 'dmidecode -s system-manufacturer',
'os_release': 'cat /etc/redhat-release',
'MAC': 'cat /sys/class/net/`[^vtlsb]`*/address',
}
8.3 设置定时任务
scanhost/tasks.py
from cmdb.settings import *
from utils import *
from models import Server
# 定时任务,定时扫描
def scan_hosts():
# 访问所有要扫描的网段/IP
for host in scanhosts:
print("正在扫描%s......" %(host))
# 获取所有可以ping通的主机IP
active_hosts = scan_active_hosts(host)
# 一次遍历判断ssh服务是否开启
for active_host in active_hosts:
if is_ssh_open(active_host):
server = Server()
# 设置IP地址
server.IP = active_host
# 执行指令
for attr, command in commands.items():
# attr ='hostname' , command = 'hostname'
# 存储主机名、操作系统.....指令执行的结果
result = login_ssh(active_host, 'root', 'westos', cmd =command)
setattr(server, attr, result)
server.save()
8.4 路由配置
cmdb/urls.py
from django.contrib import admin
from django.urls import path
from scanhost.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('scan/', scanhost),
]
8.5 视图函数
from django.shortcuts import render
# Create your views here.
from cmdb.settings import base
from scanhost.models import Server
def get_active_hosts(hosts='47.92.255.98', ports='22,80'):
pass
def is_ssh_up(host='47.92.255.98', port=22, timeout=5):
pass
def login_ssh_key(host, port, user, keyfile, command):
pass
def scanhost(request):
# 访问所有要扫描的网段/IP
for host in base.scanhosts:
print("正在扫描%s......" % (host))
# 获取所有可以ping通的主机IP
active_hosts = get_active_hosts(hosts=host)
# 一次遍历判断ssh服务是否开启
for active_host in active_hosts:
if is_ssh_up(active_host):
server = Server()
# 设置IP地址
server.IP = active_host
# 执行指令
for attr, command in base.commands.items():
# attr ='hostname' , command = 'hostname'
# 存储主机名、操作系统.....指令执行的结果
result = login_ssh_key(active_host, 22, 'root', '/mnt/id_rsa', command)
setattr(server, attr, result)
server.save()
return HttpResponse('扫描成功')
8.6 后台Admin
scanhost/admin.py
from django.contrib import admin
from models import *
# Register your models here.
admin.site.register(Server)
8.7 前端
- 目前处于待开发状态(欢迎大家合作开发!)
templates/scanhost/scan.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>scanhost</title>
</head>
<body>
<div>
<section class="content-header">
<h1>
资产总表
<small>assets list</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
<li class="active">资产总表</li>
</ol>
</section>
</div>
</body>
</html>