2024-06-12 05:05:17 发布
网友
在Perl中,如果我以编程方式执行脚本并向其传递密码,我会这样做:
my $result = qx { "Calling some script which prompts for a user and password" <<EOF administrator password EOF };
它在捕获输出时执行以下操作:
我可以知道这个在Python中的等价物吗?在
如果我没听错你的问题,你就试着在它自己的进程中启动一个外部脚本,并通过它的标准输入发送一些数据-密码。在
在Python中,这是使用^{}模块完成的。此模块运行外部脚本,并具有stdin、stdout和{}参数。在
stdin
stdout
例如,假设脚本是md5sum。您想发送密码secret:
md5sum
secret
>>> import subprocess as sp >>> p = sp.Popen('md5sum', stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE) >>> p.communicate(input="secret") # < Your password ('5ebe2294ecd0e0f08eab7690d2a6ee69 -\n', '') # < Process output
p.communicate()返回(stdout,stderr)元组,这对处理脚本输出很有用。在
p.communicate()
stderr
您还可能发现this answer很有用。在
如果我没听错你的问题,你就试着在它自己的进程中启动一个外部脚本,并通过它的标准输入发送一些数据-密码。在
在Python中,这是使用^{} 模块完成的。此模块运行外部脚本,并具有}参数。在
stdin
、stdout
和{例如,假设脚本是
md5sum
。您想发送密码secret
:p.communicate()
返回(stdout
,stderr
)元组,这对处理脚本输出很有用。在您还可能发现this answer很有用。在
相关问题 更多 >
编程相关推荐