zoukankan      html  css  js  c++  java
  • Django SimpleCMDB API

    编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getjson 时,返回 json 格式的主机组和组成员信息:

    [root@localhost SimpleCMDB]$ cat SimpleCMDB/urls.py
    ....
    urlpatterns = patterns('',
        ....
        url(r'^hostinfo/getjson/$', 'hostinfo.views.getjson'),
    )
    [root@localhost SimpleCMDB]$ cat hostinfo/views.py
    from django.shortcuts import render
    from django.http import HttpResponse
    from hostinfo.models import Host, HostGroup
    import json
    
    ....

    def getjson(request): data = [] host_group = HostGroup.objects.all() for group in host_group: result = {'groupname': group.group_name, 'members': []} for hosts in group.group_members.all(): hostinfo = {'hostname': hosts.hostname, 'ip': hosts.ip, 'vendor': hosts.vendor, 'product': hosts.product, 'sn': hosts.sn, 'cpu_model': hosts.cpu_model, 'cpu_num': hosts.cpu_num, 'memory': hosts.memory, 'osver': hosts.osver} result['members'].append(hostinfo) data.append(result) return HttpResponse(json.dumps(data))

    编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getshell 时,返回 shell 格式的主机组和组成员信息(返回 shell 格式的信息,主要是为了我们能在 Linux 下使用 grep 、awk 等对返回的数据做进一步处理):

    [root@localhost SimpleCMDB]$ cat SimpleCMDB/urls.py
    ....
    urlpatterns = patterns('',
        ....
        url(r'^hostinfo/getshell/$', 'hostinfo.views.getshell'),
    )
    [root@localhost SimpleCMDB]$ cat hostinfo/views.py
    from django.shortcuts import render
    from django.http import HttpResponse
    from hostinfo.models import Host, HostGroup
    import json
    
    ....def getshell(request):
        data = ''
        host_group = HostGroup.objects.all()
        for group in host_group:
            groupname = group.group_name
            for hosts in group.group_members.all():
                hostname = hosts.hostname
                ip = hosts.ip
                vendor = hosts.vendor
                product = hosts.product
                sn = hosts.sn
                cpu_model = hosts.cpu_model
                cpu_num = hosts.cpu_num
                memory = hosts.memory
                osver = hosts.osver
                data += groupname + ' ' + hostname + ' ' + ip + ' ' + osver + ' ' + memory + '
    '
    
        return HttpResponse(data)

        

  • 相关阅读:
    信息收集-DNS
    Xshell下载
    JSP
    本地网络配置
    P1485 火枪打怪
    P4155 [SCOI2015]国旗计划
    P1017 [NOIP2000 提高组] 进制转换
    P1013 [NOIP1998 提高组] 进制位
    P1011 [NOIP1998 提高组] 车站
    CF841B Godsend
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10351404.html
Copyright © 2011-2022 走看看