django搭建一个资产管理系统3.IP管理

本文介绍了如何使用Django框架开发一个自动化IP管理模块。通过创建应用、设置模型、数据库操作,结合nmap和snmpwalk获取IP信息,并利用adminLTE搭建前端界面,实现了IP的自动化管理和展示。最后,文章提及了部署到服务器的后续计划。
摘要由CSDN通过智能技术生成

承上启下,接下来进行自动化IP管理模块的开发

按照传统模式的话,新增了一台server如果在Excel表(或者其他类似的)对IP进行管理的话很麻烦,因为稍不注意就忘记了进行新增IP的操作了,开发一个自动化的IP管理网页就很有必要了

首先新建一个app

python manage.py startapp ipmanage

设置模型

进入ipmanage/models.py文件,

from django.db import models
#from django.contrib.auth.models import Ipmanager
# Create your models here.

class Ipmanage(models.Model):
    id = models.IntegerField('序号', primary_key=True)
    team = models.CharField('部门',max_length=255)
    useable_officeip=models.CharField('可用officeip',max_length=255)
    officeip = models.CharField('已用officeip', max_length=255)
    officeip_hostname=models.CharField('officeip主机名',max_length=255)
    officeip_ostype=models.CharField('officeip操作系统',max_length=255)
    useable_sfcip = models.CharField('可用sfc_ip', max_length=255)
    sfcip = models.CharField('已用sfc_ip', max_length=255)
    sfcip_hostname = models.CharField('sfcip主机名', max_length=255)
    sfcip_ostype = models.CharField('sfcip操作系统', max_length=255)
    useable_pdcaip = models.CharField('可用pdca_ip', max_length=255)
    pdcaip= models.CharField('已用pdca_ip', max_length=255)
    pdcaip_hostname = models.CharField('pdcaip主机名', max_length=255)
    pdcaip_ostype = models.CharField('pdcaip操作系统', max_length=255)
    useable_iloip=models.CharField('可用iloip',max_length=255)
    iloip=models.CharField('已用iloip',max_length=255)
    iloip_hostname=models.CharField('iloip主机名',max_length=255)
    iloip_ostype=models.CharField('iloip操作系统',max_length=255)
    class Meta:
        ordering = ['useable_officeip']
        db_table = 'ipmanage'
        verbose_name = "IP管理"
        verbose_name_plural = "IP管理"


数据库用之前的就行

此处不需要在进行更改

然后注册app

在settings的下面部分添加‘login’,建议在最后添加个逗号。

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'login',
    'ipmanage'
]

然后创建记录和数据表

执行下面两个命令

python manage.py makemigrations
python manage.py migrate
D:\aworkstation\sams>python manage.py makemigrations
Migrations for 'ipmanage':
  ipmanage\migrations\0001_initial.py
    - Create model Ipmanage
Migrations for 'login':
  login\migrations\0002_auto_20191004_1716.py
    - Change Meta options on user

D:\aworkstation\sams>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.c
om/en/2.2/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, ipmanage, login, sessions
Running migrations:
  Applying ipmanage.0001_initial... OK
  Applying login.0002_auto_20191004_1716... OK

然后在sams新建一个information文件夹,新建一个insert_useableip.py文件

import pymysql
import IPy

officefrontips=['10.244.220.','10.244.221.']
for officefrontip in officefrontips:
    for lastip in range(21,255):
        if officefrontip=='10.244.220.':
            sfcfrontip='172.18.240.'
            pdcaip='172.28.8.'+str(lastip)
            iloip=''
            if lastip in range(21,91):
                theteam="SFC"
            elif lastip in range(91,146):
                theteam="WEB"
            elif lastip in range(146, 156):
                theteam = "ERP"
            elif lastip in range(156,255):
                theteam="ITIC"
        elif officefrontip=='10.244.221.':
            sfcfrontip='172.18.241.'
            pdcaip=''
            iloip='192.166.6.'+str(lastip)
            if lastip in range(21, 61):
                theteam = "DCS"
            elif lastip in range(61, 71):
                theteam = "B2B"
            elif lastip in range(71, 140):
                theteam = "DBA"
            elif lastip in range(141, 255):
                theteam = "Server"
        officeip = officefrontip + str(lastip)
        sfcip=sfcfrontip+str(lastip)
        db = pymysql.connect('10.244.220.220', 'root', 'password', 'sams')
        cursor = db.cursor()
        sql = "INSERT INTO ipmanage (team,useable_officeip,useable_sfcip,useable_pdcaip,useable_iloip) VALUE ('%s','%s','%s','%s','%s')" % (theteam, officeip,sfcip,pdcaip,iloip)
        try:
            cursor.execute(sql)
            db.commit()
        except:
            # 如果发生异常,则回滚
            db.rollback()

        db.close()

用于在数据库中插入相关的值(规划的使用部门以及非交换机/路由器保留的IP),然后就可以通过sqlyog或者直接select * from ipmanage就可看到表已经更新了

dfdd15778d16f68db857e1c6003e856b3a0.jpg

然后新建一个officeip.py通过nmap判断所属ip的操作系统,然后通过snmpwalk判断所属ip的文件名

import sys
import nmap
import os
import csv
import pymysql
import time
from multiprocessing import  Process

def scanip(allhost):
    print("此时正在调用这个")
    port='22,443,3389,3011,5900,8100,17988,5001,8443,3031,3128'
    print("allhost: %s"%(allhost))
    for hosts in allhost:
        print("hosts: %s"%(hosts))
        try:
            #创建端口扫描对象
            nm = nmap.PortScanner()
        except nmap.PortScannerError:
            print('Nmap not found', sys.exc_info()[0])
            sys.exit(0)
        except Exception as e:
            print("Unexpected error:", sys.exc_info()[0])
            print(str(e))
            sys.exit(0)

        try:
            #调用扫描方法,参数指定扫描主机hosts,nmap扫描命令行参数arguments
            nm.scan(hosts=hosts, arguments=' -v -sS -p ' + port)
        except Exception as e:
            print("Scan error:" + str(e))

        for host in nm.all_hosts():
            print("host " + host)
            #print('---------------------------------------------------------------------')
            #输出主机状态,如up、down
            #print('%s : %s' % (host,nm[host].state()))
            usedip=nm[host].state()
            if usedip=='up':
                #print(host)
                #print("22 : "+nm[host]['tcp'][22]['state'])
                itswindows=nm[host]['tcp'][3389]['state']
                itsilo=nm[host]['tcp'][17988]['state']
                itsesxi=nm[host]['tcp'][8100]['state']
                itsidrac=nm[host]['tcp'][5900]['state']
                itscitrix=nm[host]['tcp'][3011]['state']
                its3par=nm[host]['tcp'][5001]['state']
                itslinux=nm[host]['tcp'][22]['state']
                itshttps=nm[host]['tcp'][443]['state']
                its8443 = nm[host]['tcp'][8443]['state']
                itstape = nm[host]['tcp'][3031]['state']
                itspve = nm[host]['tcp'][3128]['state']
                if itswindows=='open' :
                    ostype="windows"
                elif itsilo=='open':
                    ostype='ilo'
                elif itsesxi=='open':
                    ostype='esxi'
                elif itsidrac=='open' and itshttps=='open':
                    ostype='idrac'
                elif itscitrix=='open':
                    ostype='load balance'
                elif its3par=='open':
                    ostype='3par'
                elif itstape == 'open':
                    ostype = '磁带管理机'
                elif itspve == 'open':
                    ostype = 'PVE'
                elif itslinux=='open' or itslinux=='filtered':
                    ostype='linux'
                else:
                    ostype='otheros'
                #print(ostype)
                #print(host+" : "+ostype)
                if ostype=='windows' or ostype=='linux':
                    oidname = "sysname"
       
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python网络资产管理是一种利用Python语言开发的网络资产管理系统。在现代社会,随着信息技术的快速发展,网络资产的规模不断扩大,管理起来面临着许多挑战。而Python作为一种高效、易学、功能丰富的编程语言,可以帮助解决网络资产管理的问题。 首先,Python提供了丰富的网络库和模块,如socket、http、urllib等,可以方便地进行网络通信和数据获取。通过使用这些库,我们可以编写网络资产的扫描程序,定期对网络中的设备进行扫描和识别,获取设备信息,如IP地址、MAC地址、操作系统类型等,以便进行资产管理和安全性评估。 其次,Python还提供了强大的数据处理和分析工具,如pandas、numpy等。我们可以借助这些库,对扫描获取的网络资产信息进行分析和处理,统计各种类型设备的数量、分布情况等。同时,Python还支持可视化库,如matplotlib、seaborn等,可以将分析结果以图表的形式呈现,更直观地展示网络资产的状态。 另外,Python还支持多线程和异步编程,可以提高网络资产管理系统的并发性能。我们可以使用多线程技术,同时对多个设备进行信息获取和处理,提高扫描和管理的效率。同时,借助异步编程的特性,我们可以处理大规模网络资产数据的收集和处理,提高系统的响应速度。 此外,Python还有丰富的第三方库和框架,如Django、Flask等,可以帮助开发者快速搭建网络资产管理系统的前后端结构和交互功能。 综上所述,Python作为一种强大的编程语言,提供了丰富的库和工具,可以帮助实现网络资产管理系统的各种功能,包括网络设备的扫描和识别、数据处理和分析、可视化展示、并发性能优化等。因此,Python网络资产管理具有很大的应用潜力和发展前景。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值