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

1
<br>

1、Get_RemoteAPP.ps1 

1
2
3
4
5
6
7
8
9
10
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import  subprocess
import  json
 
def  NewApp(param1):
     try :
         args  =  [r "powershell" , r "C:\flask_remoteAPP_http\PosershellModule\New_RemoteAPP.ps1" ,param1]
         =  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]
         =  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" ]
         =  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  in  f1.readlines():
         =  i.decode( "gbk" ).split( "," )
         data[a[ 1 ]]  =  a[ 0 ]
     appData[ "all" =  data
     jsondata  =  json.dumps(appData)
     return  jsondata

5、run.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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__" :
     =  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中文名'}}



本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1890352

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值