博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python调用powershell,flask框架实现操作RemoteAPP接口
阅读量:7139 次
发布时间:2019-06-28

本文共 6722 字,大约阅读时间需要 22 分钟。

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

转载地址:http://kjtrl.baihongyu.com/

你可能感兴趣的文章
上传图片 以及做成缩略图
查看>>
封装和多态
查看>>
POJ - 3041 Asteroids 【二分图匹配】
查看>>
luogu P4198 楼房重建——线段树
查看>>
使用property为类中的数据添加行为
查看>>
程序设计基础知识
查看>>
复变函数与积分变换
查看>>
12. 断点续传的原理
查看>>
C#基础:多态:基类可以定义并实现虚(virtual)方法,派生类可以重写(override)这些方法...
查看>>
Visifire图表
查看>>
python常用模块之paramiko与ssh
查看>>
AES算法在Python中的使用
查看>>
动手动脑3
查看>>
HDU 3397 Sequence operation(线段树区间染色加区间合并)
查看>>
【随笔】写下现在所想的,开始写博客
查看>>
linux命令之vi文本编辑器
查看>>
WPF-WrapPanel
查看>>
大家好
查看>>
iOS 疑难杂症 — — UITableView 添加 tableFooterView 旋转屏幕后收不到点击事件!!!...
查看>>
asp.net 8 Request,Response,Server
查看>>