Python中的CMDB自动化资产扫描Django项目

本文介绍如何使用Python的nmap模块扫描网络,结合paramiko进行SSH远程登录,然后利用Django框架创建CMDB项目,包括数据库设置、模型创建、前端网页和后端逻辑的编写,实现自动化资产扫描。
摘要由CSDN通过智能技术生成

nmap模块
安装nmap模块

pip3 install python-nmap -i https://pypi.douban.com/simple

扫描网段存活的IP

import nmap
nm = nmap.PortScanner()
result = nm.scan(hosts='172.25.254.0/24', arguments='-n -sP')
print(result)
hosts = nm.all_hosts()
print(hosts)

扫描是否为Linux系统

import telnetlib
import re
tn = telnetlib.Telnet(host='172.25.10.1',port=22,timeout=5)
tn_result = tn.read_until(b'\n',timeout=5).decode('utf-8')
ssh_result = re.search('SSH',tn_result)
if ssh_result:
    print('linux')
else:
    print('other')

基于paramiko实现ssh客户端密码远程登陆

import paramiko
def login_ssh_passwd(hostname='172.25.10.1',
                     port=22,
                     username='root',
                     password=None,
                     command='hostname'):
    client=paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname=hostname,port=port,
                   username=username,password=password)
    stdin,stdout,stderr = client.exec_command(command)
    return stdout.read().decode('utf-8')
if __name__ == '__main__':
    result = login_ssh_passwd(password='westos')
    print(result)

基于paramiko实现ssh客户端密钥远程登陆

import paramiko
def login_ssh_key(hostname='172.25.10.1',
                     port=22,
                     username='root',
                     keyfile=None,
                     command='hostname'):
    client=paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    private = paramiko.RSAKey.from_private_key_file(keyfile)
    client.connect(hostname=hostname,port=port,
                   username=username,pkey=private)
    stdin,stdout,stderr = client.exec_command(command)
    return stdout.read().decode('utf-8')
if __name__ == '__main__':
    result = login_ssh_key(keyfile='/root/.ssh/id_rsa')
    print(result)

Django框架的安装

pip3 install django==3 -i https://pypi.douban.com/simple

Django项目创建
在这里插入图片描述
测试django项目是否创建成功
运行

python3 manage.py runserver

进入浏览器输入返回的IP端口
在这里插入图片描述
创建CMDB项目

python3 manage.py startapp CMDB

创建数据库
安装mysql数据库完成安全初始化之后进入数据库创建CMDB库

create database CMDBDJANGO charset=utf8;

创建可以远程登陆的root用户,并且授权

create user root@'%' identified by 'westos';
grant all on CMDBDJANGO.* to 'root'@'%';

项目开始
修改设置
django/settings.py

"""
Django settings for CMDB project.
Generated by 'django-admin startproject' using Django 3.0.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'rs8kcy61x9v9o-o=u0_i)s(nha2ssc5nq^bhknmpo*1i-g=y=b'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值