【发布时间】:2018-05-02 23:38:56
【问题描述】:
有没有办法将一些runas=True arg 传递给python 中的subprocess.run 函数?我想以管理员身份运行一个进程(提升它)。感谢您的回答:)\
编辑:使用 Windows 操作系统。
【问题讨论】:
-
操作系统在这里很重要。
标签: python subprocess admin
有没有办法将一些runas=True arg 传递给python 中的subprocess.run 函数?我想以管理员身份运行一个进程(提升它)。感谢您的回答:)\
编辑:使用 Windows 操作系统。
【问题讨论】:
标签: python subprocess admin
由于没有指定操作系统,我以MS Windows操作系统为例
Windows有一个命令行实用程序“Run as”,可以作为
runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user:<UserAccountName> "<ProgramName> <PathToProgramFile>"
供进一步参考 https://technet.microsoft.com/en-us/library/cc771525.aspx
您可以在下面的代码中使用它
import subprocess as sp
prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
prog.stdin.write('password')
prog.communicate()
【讨论】:
如果您想以同一用户但具有管理员权限运行命令
请参考这个解决方案:
os.system(r'''
Powershell -Command "& { Start-Process \"notepad.exe\"
-ArgumentList @(\"C:\\Windows\\System32\\drivers\\etc\\hosts\")
-Verb RunAs } " '''
原始答案可以在这里找到https://superuser.com/a/753600/1088510
【讨论】: