如何将python进程作为另一个Windows us运行

2024-06-20 15:11:51 发布

您现在位置:Python中文网/ 问答频道 /正文

使用subprocess.Popen我们可以调用当前登录用户凭据下的任何可执行文件,如下所示:

import subprocess 
cmd = ['C:\Windows\system32\executable.exe']
proc = subprocess.Popen(cmd, cwd=os.path.dirname(RAR_EXE), stdout=subprocess.PIPE)
output = proc.stdout.read()

假设用户名和密码已知,是否可以在另一个用户凭据下调用并执行相同的executable.exe,例如:

^{pr2}$

Tags: path用户importcmd可执行文件oswindowsstdout
1条回答
网友
1楼 · 发布于 2024-06-20 15:11:51

这个问题实际上与Python没有任何关系。您只是询问如何作为另一个用户从Windows命令行执行命令。runas程序将为您完成此操作:

链接中给出的用法:

runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user: " "

其中:

  • /profile
    Loads the user's profile. This is the default. This parameter cannot be used with the /netonly parameter.
  • /no profile
    Specifies that the user's profile is not to be loaded. This allows the application to load more quickly, but it can also cause a malfunction in some applications.
  • /env
    Specifies that the current network environment be used instead of the user's local environment.
  • /netonly
    Indicates that the user information specified is for remote access only. This parameter cannot be used with the /profile parameter.
  • /savecred
    Indicates if the credentials have been previously saved by this user. This parameter is not available and will be ignored on Windows Vista Home or Windows Vista Starter Editions. This parameter cannot be used with the /smartcard parameter.
  • /smartcard
    Indicates whether the credentials are to be supplied from a smartcard. This parameter cannot be used with the /savecred parameter.
  • /showtrustlevels
    Displays the trust levels that can be used as arguments to /trustlevel.
  • /trustlevel
    Specifies the level of authorization at which the application is to run. Use /showtrustlevels to see the trust levels available.
  • /user: " "
    Specifies the name of the user account under which to run the program, the program name, and the path to the program file. The user account name format should be @ or \.
  • /?
    Displays help at the command prompt.

似乎没有任何内置的方式来提供用户的密码,因此您必须设置输入和输出管道,以便在提示时提供密码。使用Pexpect您可能会发现这项任务更容易,这是一个第三方模块,用于自动化子进程键盘交互。在

相关问题 更多 >