【问题标题】:How to handle keyboard-interactive authentication prompts in powershell?如何在 powershell 中处理键盘交互式身份验证提示?
【发布时间】:2017-07-20 05:35:23
【问题描述】:

我正在使用带有 SSH.Net 库的 powershell 来连接到 ssh 服务器并运行命令。大部分情况下都可以正常工作,除了我的系统只启用了键盘交互式身份验证(我无法更改)。

我发现 an answer 描述了如何在 C# 中使用 EventHandler 来执行键盘交互类型身份验证,但我无法成功地将其移植到 powershell。这似乎与我处理事件的方式有关,但我不确定到底是什么。任何帮助将不胜感激!

当我尝试运行以下代码时,我收到此错误:

Exception calling "Connect" with "0" argument(s): "Value cannot be null.
Parameter name: data"
At C:\Users\nathan\Documents\ssh-interactive-test.ps1:35 char:1
+ $Client.connect()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

这是我正在运行的相关代码部分:

$scriptDir = $(Split-Path $MyInvocation.MyCommand.Path)
[reflection.assembly]::LoadFrom((Resolve-Path "$scriptDir\Renci.SshNet.3.5.dll")) | Out-Null

$ip = "ip address"
$user = "username"
$pass = "password"

$kauth = New-Object Renci.SshNet.KeyboardInteractiveAuthenticationMethod($user)
$pauth = New-Object Renci.SshNet.PasswordAuthenticationMethod($user, $pass)
$action = { 
    foreach ($prompt in $event.SourceEventArgs.Prompts) {
        if ($prompt.Request -like 'Password:*') {
            $prompt.Response = $pass
        }
    }
}
$oe = Register-ObjectEvent -InputObject $kauth -EventName AuthenticationPrompt -Action $action
$connectionInfo = New-Object Renci.SshNet.ConnectionInfo($ip, 22, $user, $pauth, $kauth)
$Client = New-Object Renci.SshNet.SshClient($connectionInfo)
$Client.connect()

感谢阅读!

【问题讨论】:

  • 根据错误,连接方法似乎需要一个名为 data 的输入,您需要将其放在括号内。建议查看该方法的文档以确定“数据”应该是什么类型/内容。
  • 这是我的想法之一,但是我链接到的工作 C# 代码没有任何称为数据的此类输入。

标签: c# powershell events eventhandler ssh.net


【解决方案1】:

此交互式键盘登录将适用于您在 PowerShell 中。来自 Renci.SshNet 的 C# 代码示例的想法 下面将运行一个示例命令并获取每一行的输出:

$scriptDir = $(Split-Path $MyInvocation.MyCommand.Path)
$ip = "ip address"
$user = "username"
$pass = "password"
$port = 22
Add-Type -Path "$scriptDir\Renci.SshNet.dll"

$action = {
    param([System.Object]$sender, [Renci.SshNet.Common.AuthenticationPromptEventArgs]$e)
    foreach ($prompt in $e.Prompts) {
        if ($prompt.Request.tolower() -like '*password:*') {
            prompt.Response = $pass;
        }
    }
}
$connectionInfo = [Renci.SshNet.KeyboardInteractiveConnectionInfo]::new($ip, $port, $user);
$oe = Register-ObjectEvent -InputObject $connectionInfo -EventName AuthenticationPrompt -Action $action
[Renci.SshNet.SshClient]$client = [Renci.SshNet.SshClient]::new($ip, $port, $user,$pass);
$client.Connect();
[Renci.SshNet.ShellStream]$shellStream = $client.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
$shellStream.WriteLine("ps ax");
$result = $shellStream.ReadLine([System.TimeSpan]::FromMilliseconds(200));
while($result -ne $null) {
    Write-Host $result;
    if($result.Length -gt 1) {
        $result = $shellStream.ReadLine([System.TimeSpan]::FromMilliseconds(200));
    }
}
$client.Disconnect();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-18
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多