I'm using powershell with the SSH.Net library to connect to ssh servers and run commands. Mostly that works fine, except I have a system that only has keyboard-interactive authentication enabled (and I can't change that).
I found an answer that describes how to use an EventHandler in C# to perform keyboard-interactive type authentication, but I haven't been able to successfully port that to powershell. It appears to be something with the way that I'm handling the event, but I'm not sure exactly what. Any help would be greatly appreciated!
When I try running the below code, I get this error:
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
This is the relevant section of code that I'm running:
$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()
Thanks for reading!