【问题标题】:Python: Give input for a subprocessPython:为子进程提供输入
【发布时间】:2014-11-10 02:01:49
【问题描述】:

我必须使用 python 脚本创建一个具有管理员权限的新进程。我在网上搜索了这个主题,发现我必须使用runas命令。对于这个命令,我必须给一个用户,并且在运行时,脚本将等待密码,我希望尽可能自动地执行此操作。

这是我的代码:

import subprocess

p = subprocess.Popen(['runas', '/user:Armando', szCmdCommand], stdin = subprocess.PIPE)
p.stdin.write('password')

我尝试了p.communicate(input='password') 而不是.write,但它不起作用,因为它没有写入密码。唯一发生的事情是脚本不等待输入。它会自动输入错误的密码并无法执行我的命令。

我能做些什么来解决这个问题?

【问题讨论】:

  • 您真的要在脚本中以明文形式写入密码吗?
  • 是的。这不会是一个问题。

标签: python windows input subprocess pipe


【解决方案1】:

希望输入密码的程序通常不会为此使用从标准输入 (stdin) 进行简单读取,而是直接控制终端(如您在 source code of the getpass module 中所见)。

使用其他东西,例如:

【讨论】:

  • 谢谢。这正是我正在寻找的东西,但它不起作用。它说'sanur' is not recognized as an internal or external command
猜你喜欢
  • 1970-01-01
  • 2019-12-05
  • 2011-10-17
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
相关资源
最近更新 更多