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',