前言
最近想通过Python来调用LoadRunner执行命令以及结果分析,但是由于和谐版原因,LoadRunner需要用管理员权限来运行,手动执行可以右键-管理员身份运行,那Python脚本在执行的时候如何获取管理员权限呢?(或许有人说直接管理员身份运行Python脚本即可,但是如果是通过IIS,FastCGI来调用呢?)。所以网上找了很多方案,下面对各种方案进行了说明,最终找到了两种可行方案。
1. 调用常规的windows命令 adb_proc = subprocess.Popen(cmd_list,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell = False)
(__output,__error) = adb_proc.communicate()
self.__return = adb_proc.returncode
其中shell=False的时候cmd_list是一个列表,shell=True的时候cmd_list是一个字符串,即要执行的命令,例如
subprocess.Popen(["adb","-s","device_id"], shell=Fale)
subprocess.Popen("adb -s device_id", shell=True)
至于subprocess模块的详细介绍,参考Python网站
2. 通过管理员来调用windows命令
1. 手工方式:使用runas
通过命令行来执行
C:\Users\AppAdmin>runas /user:Administrator dir
输入 Administrator 的密码:
试图将 dir 作为用户 "JTCRTVURA187\Administrator" 启动...
runas的具体参数可以参考微软的文档
2. 如何通过Python来调用
手工方式可以解决我们的问题,但是需要输入密码,需要另外的方案,下面整理了几种方案
1. 使用subprocess调用runas
使用stdin.write方式输入密码:无法满足需求
import subprocess
args = ['command-that-requires-password', '-user', 'me']
proc = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
proc.stdin.write('mypassword\n')
proc.stdin.flush()
stdout, stderr = proc.communicate()
print stdout
print stderr
使用SendKeys模拟输入密码:无法满足需求
import SendKeys
import subprocess
password = "PASSWORD"
command = "runas /user:USERNAME Notepad.exe"
subprocess.Popen(command)
send = """%s{ENTER}""" % (password)
SendKeys.SendKeys(send)
使用runas的/savecred参数:满足需求
/savecred参数会保存输入的密码,要管理保存的密码,在控制面板-凭据管理器中可以查看。
首先执行一次命令然后保存密码
runas /savecred /user:Administrator cmd
输入密码回车,密码就保存到了本机,下次可以直接使用runas而不用输入密码,再使用Python脚本进行调用即可。
subprocess.Popen("runas /savecred /user:Administrator cmd", shell=True)
2. 使用psexec命令
下面使用了另一个模块,具体查看官方链接,首先下载链接的文件,解压之后放进c:\windows\system32文件夹下
psexec -u user -p password cmd
同上,直接使用subprocess调用即可。
如果提示不是内部或者外部命令,可以使用绝对路径C:\\Windows\\SysNative\\PsExec.exe,原因是64位系统,会将命令重定向到SysWOW64。所以可以访问SysNative下真正的命令。 参考连接
3. 使用pexpect库 pip install pexpect
相关介绍:网址,参考链接
下面是使用样例
child = pexpect.spawn('command to run')
child.expect ('Password:')
child.sendline ('foobar')
但是上面的命令是针对linux的,如果是windows需要这样来使用
from pexpect import popen_spawn
child=popen_spawn.PopenSpawn(r'runas /user:Administrator cmd')
print child.expect("输入 Administrator 的密码:")
child.send("password")
很遗憾,这种方式也不能满足需求,无法输入密码,但是这个模块提供了命令行交互很好的方式,以后在linux下可以使用。
4. 使用net use命令
网上的使用样例,但是具体如何使用没有找到很好的办法
net use \\x.x.x.x /user:domain\user password
copy *.* etc
5. 使用pywin32模块来模拟输入密码
首先下载pywin32安装,之后使用下面的代码,但是经我测试没有通过。参考链接
import win32console, win32con, time
import subprocess
username = "me"
domain = "my_domain"
password ="xxx"
free_console=True
try:
win32console.AllocConsole()
except win32console.error as exc:
if exc.winerror!=5:
raise
## only free console if one was created successfully
free_console=False
stdin=win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
p = subprocess.Popen(["runas",r"/user:{}\{}".format(domain,username),"cmd.exe"],stdout=subprocess.PIPE)
while True:
if p.stdout.read(1)==":":
for c in "{}\r".format(password): # end by CR to send "RETURN"
## write some records to the input queue
x=win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
x.Char=unicode(c)
x.KeyDown=True
x.RepeatCount=1
x.VirtualKeyCode=0x0
x.ControlKeyState=win32con.SHIFT_PRESSED
stdin.WriteConsoleInput([x])
p.wait()
break
6. 创建管理员身份执行的快捷方式
这种方式听起来应该可行,但是没有详细了解。
3. 总结
多种方式进行测试,最终可以使用的是
使用psexec命令来执行
使用runas并加上/savecred
如果有其它更好的方式,欢迎提出各种建议。