承上启下,接下来进行自动化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就可看到表已经更新了
然后新建一个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"