发布网友 发布时间:2022-04-11 00:06
共2个回答
懂视网 时间:2022-04-11 04:27
lib.config.config import settings import json import requests from src.plugins import PluginsManger if __name__ == ‘__main__‘: if settings.MODE == ‘agent‘: res = PluginsManger().execute() requests.post(‘http://127.0.0.1:8000‘, data=json.dumps(res)) else: res = PluginsManger().execute()而逻辑代码是不能写在启动文件中的,所以在src创一个文件client.py
from lib.config.config import settings import json import requests from src.plugins import PluginsManger if settings.MODE == ‘agent‘: res = PluginsManger().execute() requests.post(‘http://127.0.0.1:8000‘, data=json.dumps(res)) else: res = PluginsManger().execute()
但是以上又是面向过程的思想,所以要面向对象的思想
服务端的IP地址写在custom_settings。py里
import os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) MODE = ‘agent‘ SSH_USERNAME = ‘root‘ SSH_PASSWORD = ‘123‘ SHH_PORT = 22 ### APIurl地址 API_URL = ‘http://127.0.0.1:8000/getInfo/‘ DEBUG = True PLUGINS_DICT = { ‘basic‘:‘src.plugins.basic.Basic‘, ‘board‘: ‘src.plugins.board.Board‘, ‘disk‘: ‘src.plugins.disk.Disk‘, ‘memory‘: ‘src.plugins.memory.Memory‘, ‘cpu‘:‘src.plugins.cpu.Cpu‘, ‘nic‘:‘src.plugins.nic.Nic‘ }
client.py(先对agent采集模式进行测试)
from src.plugins import PluginsManger import json import requests from lib.config.config import settings class Agent(object): #收集数据并发送 def collectAndPost(self): res = PluginsManger().execute() for k,v in res.items(): print(k,v) requests.post(settings.API_URL, data=json.dumps(res))
start.py
from src.client import Agent if __name__ == ‘__main__‘: Agent().collectAndPost()
起一个django (我的是2.0版本) 服务端,应用项目是api
from django.contrib import admin from django.urls import path,re_path from api import views urlpatterns = [ path(‘admin/‘, admin.site.urls), re_path(‘getInfo/‘, views.getInfo), ]
将settings中drf注掉
# a. request.body中永远都是有数据的
#
#
# 当 Content-Type: application/x-www-form-urlencoded, request.POST中才会有值
# 当 Content-Type‘:"application/json" 这是采集端发送数据的头形式
views.py
from django.shortcuts import render,HttpResponse # Create your views here. def getInfo(request): print(request.body) return HttpResponse(‘ok‘)
cmdb客户端代码完善2
标签:admin imp 验证 运维 加密 逻辑 服务 agent div
热心网友 时间:2022-04-11 01:35
你下个360安全卫士,他会帮你把恶意的软件和插键都清除的,要是但靠手工删除,你会有遗漏