python调用powershell,flask框架实现操作RemoteAPP接口

2017-01-09 19:20:49来源:作者:人点击

1、Get_RemoteAPP.ps1 
set-executionpolicy remotesignedImport-Module RemoteDesktopServicesfunction GetAPP(){ $result = ls -l RDS:/RemoteApp/RemoteAppPrograms/ $res = $result | ForEach-Object {$_.Name} return $res}GetAPP
2、New_RemoteAPP.ps1
set-executionpolicy remotesignedImport-Module RemoteDesktopServices$appList="C:/Program Files/PremiumSoft/Navicat Premium/navicat.exe","C:/Program Files (x86)/JetBrains/PyCharm Community Edition 2016.2.3/bin/pycharm.exe","C:/Program Files/PremiumSoft/Navicat Premium/navicat.exe","C:/Program Files (x86)/JetBrains/PyCharm Community Edition 2016.2.3/bin/pycharm.exe","C:/Program Files (x86)/OpenVPN/bin/openvpn-gui.exe","C:/Program Files (x86)/Mozilla Firefox/firefox.exe","C:/Program Files (x86)/KuGou/KGMusic/KuGou.exe","C:/Program Files (x86)/TeamViewer/TeamViewer.exe","C:/Program Files (x86)/Youdao/YoudaoNote/YoudaoNote.exe","C:/Users/Administrator.WIN-403TF0V1RLC/AppData/Local/youdao/dict/Application/YodaoDict.exe",""function CheckAppPath($app){ foreach($list in $appList){if ($list -like "*$app*"){$index = $list | % {[array]::IndexOf($appList,$_)}$appPath = $appList[$index] return $appPath } }}function New-RDSRemoteApp($appName){ #返回值说明: # 2 :程序已存在 # 1 : 添加成功 # 0 : 添加失败 if (Test-Path RDS:/RemoteApp/RemoteAppPrograms/$appName) {return 2 }else { $appPath = CheckAppPath $appNameNew-Item -path RDS:/RemoteApp/RemoteAppPrograms -Name $appName -applicationpath $appPath >$nullif (Test-Path RDS:/RemoteApp/RemoteAppPrograms/$appName) {return 1} else {return 0} }}New-RDSRemoteApp $args[0]
3、Remove_RemoteAPP.ps1
set-executionpolicy remotesignedImport-Module RemoteDesktopServicesfunction RemoveAPP($appName){ #返回值0 :软件不存在 #返回值1 : 删除成功 #返回值2 : 删除失败 if (-not (Test-Path RDS:/RemoteApp/RemoteAppPrograms/$appName)){return 0 }else{ Remove-Item -path RDS:/RemoteApp/RemoteAppPrograms/$appName -Recurse -Force -Confirm:$false | Out-Null if (-not (Test-Path RDS:/RemoteApp/RemoteAppPrograms/$appName)){return 1 }else{return 2 }}}RemoveAPP $args[0]
4、CallPowerShell.py
import subprocessimport jsondef NewApp(param1): try:args = [r"powershell", r"C:/flask_remoteAPP_http/PosershellModule/New_RemoteAPP.ps1",param1]p = subprocess.Popen(args, stdout=subprocess.PIPE)dt = p.stdout.read()return dt except Exception, e:print e return Falsedef DelApp(param1): try:args = [r"powershell", r"C:/flask_remoteAPP_http/PosershellModule/Remove_RemoteAPP.ps1",param1]p = subprocess.Popen(args, stdout=subprocess.PIPE)dt = p.stdout.read()return dt except Exception, e:print e return Falsedef GetApp(): try:args = [r"powershell", r"C:/flask_remoteAPP_http/PosershellModule/Get_RemoteAPP.ps1"]p = subprocess.Popen(args, stdout=subprocess.PIPE)dt = p.stdout.read()datalist = dt.split("/n")data = json.dumps(datalist)return data except Exception, e:print e return Falsedef QueryAllapp(): f1 = open(r"C:/flask_remoteAPP_http/PosershellModule/appInventory.txt","r") data = {} appData = {} for i in f1.readlines():a = i.decode("gbk").split(",")data[a[1]] = a[0] appData["all"] = data jsondata = json.dumps(appData) return jsondata
5、run.py
from flask import Flask,requestimport jsonfrom CallPowerShell import NewApp,DelApp,GetApp,QueryAllappapp=Flask(__name__)  @app.route('/newapp', methods=['GET','POST'])  def newapps(): try:if request.method == 'POST':jsondata = request.get_data()dictdata = json.loads(jsondata)response = NewApp(dictdata["appName"])return responseelse:mes = ''' <p><h1>Not Found</H1></p> <p>Request of this path cannot be found, or the server does not exist</p>'''return mesexcept Exception,error:print Exception,":",error @app.route('/delapp', methods=['GET','POST'])  def delapps(): try:if request.method == 'POST':jsondata = request.get_data()dictdata = json.loads(jsondata)res = DelApp(dictdata['appName'])return reselse:mes = ''' <p><h1>Not Found</H1></p> <p>Request of this path cannot be found, or the server does not exist</p>'''return mesexcept Exception,error:print Exception,":",error@app.route('/getapp')def getapps(): try:res = GetApp()return resexcept Exception,error:print Exception,":",error@app.route('/queryall')def queryalls(): try:res = QueryAllapp()return resexcept Exception,error:print Exception,":",errorif __name__ == '__main__':app.run(debug=True,host='0.0.0.0')
6、client.py
#coding:utf-8import urllib2import jsondef newApp(appName): url = 'http://192.168.1.115:5000/newapp' #url = 'http://192.168.1.115:5000/delapp' data = {'appName': appName} headers = {'Content-Type': 'application/json'} req = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(req) return response.read()def delApp(appName): url = 'http://192.168.1.115:5000/delapp' data = {'appName': appName} headers = {'Content-Type': 'application/json'} req = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(req) return response.read()def getApp(): url = 'http://192.168.1.115:5000/getapp' req = urllib2.Request(url=url) response = urllib2.urlopen(req) return response.read()def queryAllApp(): url = 'http://192.168.1.115:5000/queryall' req = urllib2.Request(url=url) response = urllib2.urlopen(req) return response.read()if __name__ == "__main__": a = queryAllApp() b  = json.loads(a) print b
7、接口说明1、添加APP接口请求方式:POST传送数据类型:JSON请求URL:http://192.168.1.115:5000/newapp请求参数:{'appName':程序别名}返回数据类型:字符串返回结果:返回 "1" 添加成功返回 "2" 程序已存在返回 "0" 添加失败2、删除APP接口请求方式:POST传送数据类型:JSON请求URL:http://192.168.1.115:5000/delapp请求参数:{'appName':程序别名}返回数据类型:字符串返回结果:返回 "1" 删除成功返回 "2" 删除失败返回 "0" app不存在3、获取已添加的APP列表请求方式:GET请求URL:http://192.168.1.115:5000/getapp请求参数:无参数返回数据类型:json返回数据:['app1','app2','app3']4、获取可进行添加的APP列表(包含已添加)的APP列表请求方式:GET请求URL:http://192.168.1.115:5000/getapp请求参数:无参数返回数据类型:json返回数据:{'all':{'app1别名':'app1中文名','app2别名':'app2中文名'}}本文出自 “运维杂谈Q群:223843163” 博客,请务必保留此出处http://freshair.blog.51cto.com/8272891/1890352

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台