使用 powershell 将文本发送到 Python 进程的密码 STDIN
Send text to password STDIN of Python process with powershell
我正在尝试创建一个 powershell 脚本,它将来自凭据管理器的密码输入到 Python 脚本的密码输入中。在 this older post 中,我找到了一些关于如何使用 Powershell 启动进程然后在 STDIN 中输入一些文本的信息,但由于某种原因,这种方法对我不起作用。我执行 python 脚本,它一直在等待在 Powershell 命令行 window.
中输入密码
这是代码,它正确执行了要求输入密码的 Python 脚本,但之后没有任何反应。我可以手动输入密码并点击回车,但这当然不是目的。然后我可以自己执行python脚本。
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. $executingScriptDirectory\CredMan.ps1
$launcherScript = Join-Path $executingScriptDirectory "launcher.py"
$credTarget = 'some-target-in-credential-manager'
Write-Host "Loading password from Windows credmgr entry '$credTarget' ..."
[object] $cred = Read-Creds $credTarget
if ($cred -eq $null)
{
Write-Host ("No such credential found: {0}" -f $credTarget)
Exit 2
}
# Found the credential; grab the password and boot up the launcher
[string] $password = $cred.CredentialBlob
Write-Host "Launching $launcherScript ..."
Write-Host "Password: '$password'"
$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.Arguments = "$launcherScript "
$psi.FileName = "python.exe";
$psi.UseShellExecute = $false; # start the process from its own executable file
$psi.RedirectStandardInput = $true; # enable the process to read from stdin
$p = [System.Diagnostics.Process]::Start($psi);
Start-Sleep -s 2 # wait 2 seconds so that the process can be up and running
$p.StandardInput.WriteLine($password);
$p.WaitForExit()
可能是什么问题?在 python 脚本中使用此行请求密码,因此使用 getpass 模块。
password = getpass.getpass("Enter your password: ")
感谢您的帮助。
如果您需要更多信息,请索取 :)。
我想 Python 进程不会从 STDIN 流中读取密码,而是直接从进程所连接的终端读取密码。此终端流不受您在启动子进程之前碰巧安装的任何重定向的影响,因此写入进程的 STDIN 不会影响这一点。事实上,你可以使用键盘直接在 Python 进程中输入你的密码,并且它接受它,这证明我是对的,我想说。
在您的情况下,您需要调整 Python 过程以从其他地方读取 PW,例如。 G。通过传递一个特殊选项(当然完全取决于您的 Python 过程)或通过修补 Python 源本身。
也许还有 Windows 特定的方法来模拟键盘按下,但我认为这是一个非常丑陋的 hack,因此不推荐。
Alfe,谢谢你为我指明了解决这个问题的正确方向。
我调整了python脚本,让它接受命令行参数,密码参数可以在-p选项后给出,所以像这样:
$ script.py -p "password"
为了从 powershell 脚本执行此操作,我使用此代码首先从 Windows 凭据管理器中获取凭据,并将其作为参数提供给 python 脚本。
我使用现有脚本从凭据管理器中获取凭据,即 CredMan.ps1 script。
# Get the path of where this script is being invocated to reference to the script files in the same directory as this script.
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
# Include external script to speak with Credential Manager
. $executingScriptDirectory\CredMan.ps1
$launcherScript = Join-Path $executingScriptDirectory "script.py"
$credentialTarget = "name-of-credential-in-manager"
[object] $cred = Read-Creds $credentialTarget
if ($cred -eq $null) {
Write-Host ("No such credential found: {0}" -f $credentialTarget)
Exit 2
}
# Found the credential; Grab the password and execute the script with the password
[string] $password = $cred.CredentialBlob
# Execute python script with password given as parameter
$exe = "python.exe"
&$exe $launcherScript -p $password
感谢您的帮助,希望您理解给出的答案。
我正在尝试创建一个 powershell 脚本,它将来自凭据管理器的密码输入到 Python 脚本的密码输入中。在 this older post 中,我找到了一些关于如何使用 Powershell 启动进程然后在 STDIN 中输入一些文本的信息,但由于某种原因,这种方法对我不起作用。我执行 python 脚本,它一直在等待在 Powershell 命令行 window.
中输入密码这是代码,它正确执行了要求输入密码的 Python 脚本,但之后没有任何反应。我可以手动输入密码并点击回车,但这当然不是目的。然后我可以自己执行python脚本。
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. $executingScriptDirectory\CredMan.ps1
$launcherScript = Join-Path $executingScriptDirectory "launcher.py"
$credTarget = 'some-target-in-credential-manager'
Write-Host "Loading password from Windows credmgr entry '$credTarget' ..."
[object] $cred = Read-Creds $credTarget
if ($cred -eq $null)
{
Write-Host ("No such credential found: {0}" -f $credTarget)
Exit 2
}
# Found the credential; grab the password and boot up the launcher
[string] $password = $cred.CredentialBlob
Write-Host "Launching $launcherScript ..."
Write-Host "Password: '$password'"
$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.Arguments = "$launcherScript "
$psi.FileName = "python.exe";
$psi.UseShellExecute = $false; # start the process from its own executable file
$psi.RedirectStandardInput = $true; # enable the process to read from stdin
$p = [System.Diagnostics.Process]::Start($psi);
Start-Sleep -s 2 # wait 2 seconds so that the process can be up and running
$p.StandardInput.WriteLine($password);
$p.WaitForExit()
可能是什么问题?在 python 脚本中使用此行请求密码,因此使用 getpass 模块。
password = getpass.getpass("Enter your password: ")
感谢您的帮助。
如果您需要更多信息,请索取 :)。
我想 Python 进程不会从 STDIN 流中读取密码,而是直接从进程所连接的终端读取密码。此终端流不受您在启动子进程之前碰巧安装的任何重定向的影响,因此写入进程的 STDIN 不会影响这一点。事实上,你可以使用键盘直接在 Python 进程中输入你的密码,并且它接受它,这证明我是对的,我想说。
在您的情况下,您需要调整 Python 过程以从其他地方读取 PW,例如。 G。通过传递一个特殊选项(当然完全取决于您的 Python 过程)或通过修补 Python 源本身。
也许还有 Windows 特定的方法来模拟键盘按下,但我认为这是一个非常丑陋的 hack,因此不推荐。
Alfe,谢谢你为我指明了解决这个问题的正确方向。
我调整了python脚本,让它接受命令行参数,密码参数可以在-p选项后给出,所以像这样:
$ script.py -p "password"
为了从 powershell 脚本执行此操作,我使用此代码首先从 Windows 凭据管理器中获取凭据,并将其作为参数提供给 python 脚本。
我使用现有脚本从凭据管理器中获取凭据,即 CredMan.ps1 script。
# Get the path of where this script is being invocated to reference to the script files in the same directory as this script.
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
# Include external script to speak with Credential Manager
. $executingScriptDirectory\CredMan.ps1
$launcherScript = Join-Path $executingScriptDirectory "script.py"
$credentialTarget = "name-of-credential-in-manager"
[object] $cred = Read-Creds $credentialTarget
if ($cred -eq $null) {
Write-Host ("No such credential found: {0}" -f $credentialTarget)
Exit 2
}
# Found the credential; Grab the password and execute the script with the password
[string] $password = $cred.CredentialBlob
# Execute python script with password given as parameter
$exe = "python.exe"
&$exe $launcherScript -p $password
感谢您的帮助,希望您理解给出的答案。