简体   繁体   English

如何以其他Windows用户身份运行python进程

[英]How to run python process as another Windows user

With the subprocess.Popen we can call any executable under the currently logged-in user credentials like so: 使用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()

Would it be possible to call and execute the same executable.exe under another user credentials assuming that the user's name and the password are known, such as: 假设已知用户名和密码,则可以在另一个用户凭据下调用并执行相同的executable.exe ,例如:

username = 'steve'
password = 'password123`

This question doesn't really have anything to do with Python. 这个问题实际上与Python没有任何关系。 You're just asking how to execute a command as another user from the Windows command line. 您只是在问如何从Windows命令行以其他用户身份执行命令。 The runas program will do that for you: runas程序将为您做到这一点:

Usage as given in the link: 链接中给出的用法:

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

Where: 哪里:

  • /profile /轮廓
    Loads the user's profile. 加载用户的个人资料。 This is the default. 这是默认值。 This parameter cannot be used with the /netonly parameter. 此参数不能与/ netonly参数一起使用。
  • /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 / ENV
    Specifies that the current network environment be used instead of the user's local environment. 指定使用当前的网络环境,而不是用户的本地环境。
  • /netonly / netonly
    Indicates that the user information specified is for remote access only. 指示指定的用户信息仅用于远程访问。 This parameter cannot be used with the /profile parameter. 此参数不能与/ profile参数一起使用。
  • /savecred / 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. 该参数不可用,在Windows Vista Home或Windows Vista Starter Editions中将被忽略。 This parameter cannot be used with the /smartcard parameter. 此参数不能与/ smartcard参数一起使用。
  • /smartcard /智能卡
    Indicates whether the credentials are to be supplied from a smartcard. 指示是否要从智能卡提供凭据。 This parameter cannot be used with the /savecred parameter. 此参数不能与/ savecred参数一起使用。
  • /showtrustlevels / showtrustlevels
    Displays the trust levels that can be used as arguments to /trustlevel. 显示可以用作/ trustlevel参数的信任级别。
  • /trustlevel / trustlevel
    Specifies the level of authorization at which the application is to run. 指定运行应用程序的授权级别。 Use /showtrustlevels to see the trust levels available. 使用/ showtrustlevels查看可用的信任级别。
  • /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. 在命令提示符下显示帮助。

It doesn't look like there's any built-in way to provide the user's password, so you'll have to set up input and output pipes to be able to provide it when prompted. 看起来似乎没有任何内置的方式可以提供用户密码,因此您必须设置输入和输出管道才能在出现提示时提供密码。 You may find this task easier with Pexpect , which is a third-party module for automating subprocess keyboard interaction. 您可能会发现使用Pexpect可以更轻松地完成此任务, Pexpect是用于自动化子流程键盘交互的第三方模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM