1. 程式人生 > >Django+ PowerShell 管理AD系統

Django+ PowerShell 管理AD系統

powershell python django

QQ群裏的Evan童鞋分享了一個很有意思的博客 http://note.youdao.com/noteshare?id=a60709c00fe88cd09155a2ef50815281 大概是如何利用Flask 調用 Powershell API 實現的一個運維管理系統。


豆子依葫蘆畫瓢,用Django成功地實現了有一個簡單的界面。 直接用Bootstrap模板弄個前端頁面,Django 框架,然後後臺調用PowerShell API實現查詢。


下面是一個簡單的demo,輸入AD的組,顯示組成員


技術分享


Django沒啥好說的,基本的MTV框架流程,主要比較好玩的是這個PowerShell API的模塊。網上有現成的HttpListener的模塊可以下載,QQ群裏的童鞋做了些修改,去掉了一個驗證的功能,如果有需求,可以自己手動添加一個函數進去。我這裏圖省事是直接用的去驗證的版本。


這個模塊下載導入之後就可以執行了,他提供了一個類似restful的接口來執行Powershell的命令,直接Http get請求對應的接口,然後返回json格式的結果

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


測試一下:

瀏覽器

技術分享

Python

技術分享




值得一提的是,具體的Powershell命令放在哪裏,我們可以在兩個地方設置。一個是直接在uri裏面 command=後面輸入,簡單的命令無所謂,但是如果命令很復雜很長的話,這裏就不是太合適了;


另外一個方式是可以在HTTPListener的模塊文件裏面直接寫個function,這樣加載的時候一起放入內存了。command=後面直接跟函數名和參數就行了。

比如說:


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


那我直接調用

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


顯示結果

技術分享



okay,基本能工作了,那麽在django上弄個界面看看吧


url.py 路由

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


views.py 視圖函數

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進行提交

{%  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 測試頁面</h1>
<h3 >查詢用戶組</h3>
            <form method="POST" action="/powershell">
                {% csrf_token %}
                <input type="text" name="caption" placeholder="組名" />
                <input type="submit" value="查詢"/>
            </form>
<br>
    <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 %}


這樣一個查詢效果就做出來了。


本文出自 “麻婆豆腐” 博客,請務必保留此出處http://beanxyz.blog.51cto.com/5570417/1979809

Django+ PowerShell 管理AD系統