python调用powershell,flask框架实现操作RemoteAPP接口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python调用powershell,flask框架实现操作RemoteAPP接口相关的知识,希望对你有一定的参考价值。
1、Get_RemoteAPP.ps1
set-executionpolicy remotesigned Import-Module RemoteDesktopServices function GetAPP(){ $result = ls -l RDS:\RemoteApp\RemoteAppPrograms $res = $result | ForEach-Object {$_.Name} return $res } GetAPP
2、New_RemoteAPP.ps1
set-executionpolicy remotesigned Import-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 $appName New-Item -path RDS:\RemoteApp\RemoteAppPrograms -Name $appName -applicationpath $appPath >$null if (Test-Path RDS:\RemoteApp\RemoteAppPrograms\$appName) { return 1 } else { return 0 } } } New-RDSRemoteApp $args[0]
3、Remove_RemoteAPP.ps1
set-executionpolicy remotesigned Import-Module RemoteDesktopServices function 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 subprocess import json def 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 False def 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 False def 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 False def 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,request import json from CallPowerShell import NewApp,DelApp,GetApp,QueryAllapp app=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 response else: mes = ‘‘‘ <p><h1>Not Found</H1></p> <p>Request of this path cannot be found, or the server does not exist</p> ‘‘‘ return mes except 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 res else: mes = ‘‘‘ <p><h1>Not Found</H1></p> <p>Request of this path cannot be found, or the server does not exist</p> ‘‘‘ return mes except Exception,error: print Exception,":",error @app.route(‘/getapp‘) def getapps(): try: res = GetApp() return res except Exception,error: print Exception,":",error @app.route(‘/queryall‘) def queryalls(): try: res = QueryAllapp() return res except Exception,error: print Exception,":",error if __name__ == ‘__main__‘: app.run(debug=True,host=‘0.0.0.0‘)
6、client.py
#coding:utf-8 import urllib2 import json def 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
以上是关于python调用powershell,flask框架实现操作RemoteAPP接口的主要内容,如果未能解决你的问题,请参考以下文章
我如何运行 python flask 并获取本地主机的链接? [复制]
Flask - 在按钮 OnClick 事件上调用 python 函数