Error occurs when using Private key to authenticate
Below is a function I've built in a Powershell script to scp file to a linux machine using Private key file.
Following is the error message generated by winscp
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0."
I face no issues when using the same userid and Private key file with putty's psftp
function scpFile
{
logWrite("SCP'ing BES UIM file $BESUIMFeedFile to $sdxHost using id $sdxUser" )
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Scp
$sessionOptions.HostName = $sdxHost
$sessionOptions.UserName = $sdxUser
$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = $true
$sessionOptions.SshPrivateKeyPath = $scpPrivateKeyPath
$session = New-Object WinSCP.Session
try
{
#Connect
$session.Open($sessionOptions)
#
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($BESUIMFeedFile, $sdxDestPath, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
logWrite ("Upload of {0} succeeded" -f $transfer.FileName)
}
}
catch
{
writeToEventLog ($($Error[0]))
}
finally
{
$session.dispose()
}
}
Following is the error message generated by winscp
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0."
I face no issues when using the same userid and Private key file with putty's psftp
function scpFile
{
logWrite("SCP'ing BES UIM file $BESUIMFeedFile to $sdxHost using id $sdxUser" )
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Scp
$sessionOptions.HostName = $sdxHost
$sessionOptions.UserName = $sdxUser
$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = $true
$sessionOptions.SshPrivateKeyPath = $scpPrivateKeyPath
$session = New-Object WinSCP.Session
try
{
#Connect
$session.Open($sessionOptions)
#
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($BESUIMFeedFile, $sdxDestPath, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
logWrite ("Upload of {0} succeeded" -f $transfer.FileName)
}
}
catch
{
writeToEventLog ($($Error[0]))
}
finally
{
$session.dispose()
}
}