使用Django實(shí)現(xiàn)管理AD系統(tǒng)

用Django實(shí)現(xiàn)一個(gè)簡(jiǎn)單的界面。 直接用Bootstrap模板弄個(gè)前端頁面,Django 框架,然后后臺(tái)調(diào)用PowerShell API實(shí)現(xiàn)查詢。

   這是部分聊天系統(tǒng),物流管理系統(tǒng),聊天系統(tǒng),電商秒殺系統(tǒng)的視頻。

**視頻,源碼在公中呺**** →→→ 代碼沒bug

在這里插入圖片描述

下面是一個(gè)簡(jiǎn)單的demo,輸入AD的組,顯示組成員

在這里插入圖片描述

Django沒啥好說的,基本的MTV框架流程,主要比較好玩的是這個(gè)PowerShell API的模塊。網(wǎng)上有現(xiàn)成的HttpListener的模塊可以下載,我做了些修改,去掉了一個(gè)驗(yàn)證的功能,如果有需求,可以自己手動(dòng)添加一個(gè)函數(shù)進(jìn)去。我這里圖省事是直接用的去驗(yàn)證的版本。

這個(gè)模塊下載導(dǎo)入之后就可以執(zhí)行了,他提供了一個(gè)類似restful的接口來執(zhí)行Powershell的命令,直接Http get請(qǐng)求對(duì)應(yīng)的接口,然后返回json格式的結(jié)果

Import-Module C:\users\yuan.li\Documents\GitHub\Powershell\HTTPListener.psm1
start-httplistener -verb -Auth None

測(cè)試一下:

瀏覽器

image

Python

在這里插入圖片描述

值得一提的是,具體的Powershell命令放在哪里,我們可以在兩個(gè)地方設(shè)置。一個(gè)是直接在uri里面 command=后面輸入,簡(jiǎn)單的命令無所謂,但是如果命令很復(fù)雜很長(zhǎng)的話,這里就不是太合適了;

另外一個(gè)方式是可以在HTTPListener的模塊文件里面直接寫個(gè)function,這樣加載的時(shí)候一起放入內(nèi)存了。command=后面直接跟函數(shù)名和參數(shù)就行了。

比如說:

function search-adgroupmemeber($group){
    Get-ADGroupMember $group | select name, SamAccountName,Distinguishedname
}

那我直接調(diào)用

http://localhost:8888/?command=search-adgroupmemeber 'domain admins'

顯示結(jié)果

okay,基本能工作了,那么在django上弄個(gè)界面看看吧

url.py 路由

url(r'^powershell', views.powershell),

views.py 視圖函數(shù)

import requests
def powershell(req):
    if req.method=="GET":
        return render(req,'powershell.html')
    elif req.method=="POST":
        name=req.POST.get("caption")
        print(name)
        res=requests.get("http://localhost:8888/?command=get-adgroupmember '%s' | select name, distinguishedname"%name)
        print(res)
        result=res.json()
        print(result)
        return render(req,'powershell.html',{'result':result})

powershell.html 模板,這里我沒用AJAX,就是直接form進(jìn)行提交

{%  extends 'base.html' %}
{% block css %}
    <style>
        .go{
            width:20px;
             border: solid 1px;
            color: #66512c;
            display: inline-block;
            padding: 5px;
        }
        .pagination .page{
            border: solid 1px;
            color: #66512c;
            display: inline-block;
            padding: 5px;
            background-color: #d6dade;
            margin: 5px;
        }
        .pagination .page.active{
            background-color: black;
            color: white;
        }
        .hide{
            display: none;
        }
        .shade{
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
            background: black;
            opacity: 0.6;
            z-index: 100;
        }
        .add-modal,.edit-modal{
            position: fixed;
            height: 300px;
            width: 400px;
            top:100px;
            left: 50%;
            z-index: 101;
            border: 1px solid red;
            background: white;
            margin-left: -200px;
        }
        .group{
            margin-left: 20px;
            margin-bottom: 15px;
        }
    </style>
{% endblock %}
{% block content %}

<h1 class="page-header">Powershell 測(cè)試頁面</h1>
<h3 >查詢用戶組</h3>
<form method="POST" action="/powershell">
{% csrf_token %}
<input type="text" name="caption" placeholder="組名" />
<input type="submit" value="查詢"/>
</form>


<table border="1">
<thead>
<tr>
<th>成員</th>
<th>DN</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for items in result %}
<tr >
<td>items.name</td>
<td>items.distinguishedname</td>
<td><a class ='update'>修改 | </a><a class="delete">刪除</a></td>
</tr>
{% endfor %}
</tbody>
</table>

{% endblock %}
{% block title%}PowerShell{% endblock %}
{% block js%}
<script>
</script>
{% endblock %}

這樣一個(gè)查詢效果就做出來了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容