[SOLVED] - Ignore password promt when useing Private/Publik Keys?

Advertisement

Muffinz
Joined:
Posts:
7
Location:
Sweden

[SOLVED] - Ignore password promt when useing Private/Publik Keys?

Hi,

I'm trying to get my Powershell script to login and upload som files on a FTP server. It works great with Putty/psftp.exe.

The connection is SFTP with a username, a Key-file that has a passphrase on it and no password.
But I can't get it to work with WinSCP .Net or the Powershell module.

This gets me a login promt at $session:
$sshKeyFinger = "ssh-rsa 2048 xx:cc:vv:bb"
$sshKeyPath = "\\UNC\Path\to\Key.ppk"
$sshpw =  ConvertTo-SecureString "Password" -AsPlainText -Force

$session = New-WinSCPSession -HostName Username@host.com -SshPrivateKeyPath $sshKeyPath  -SshPrivateKeySecurePassphrase $sshpw -SshHostKeyFingerprint $sshKeyFinger 

Send-WinSCPItem -Path \\local\unc\path\* -WinSCPSession $session -Destination /transfer

Remove-WinSCPSession -WinSCPSession $session


and with this:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "Host"
    UserName = "UserName"
    Password = "SSHKey_Passphrase"
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:cc:vv:bb"
    SshPrivateKeyPath = "\\UNC\Path\to\Key.ppk"
}

$sessionOptions.AddRawSettings("AuthKI", "0")
$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)
   
    # Your code
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
    $transferResult = $session.PutFiles("\\Local\UNC\Path\*", "/transfer", $False, $transferOptions)

    # Throw on any error
    $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

I get this error message:
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "Username".
Authenticating with public key "rsa-key-".
Authentication failed."
At line:20 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

Any idea?
Last edited by Muffinz on 2016-12-27 10:47; edited 1 time in total

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: Ignore password promt when useing Private/Publik Keys?

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Guest

Re: Ignore password promt when useing Private/Publik Keys?

martin wrote:

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Hi,

I "solved" it with this code :)

# Load WinSCP .NET assembly
Add-Type -Path "\\srv\Script\FTP\WinSCP-5.9.3-Automation\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "HostName"
    UserName = "UserName"
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    SshPrivateKeyPath = "\srv\Script\FTP\psftp\Key.ppk"
}



$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Temp\Log\Uploaded.log"
$sessionOptions.SshPrivateKeyPassphrase = "Password"
try
{
    # Connect
    $session.Open($sessionOptions)

    # Your code

    $localPath = "C:\Temp\test\*" 
    $remotePath = "/SomeRemoteFolder" 

    # Upload files, collect results 
    $transferResult = $session.PutFiles($localPath, $remotePath) 

    Write-Host ("Upload of {0} succeeded" -f 
    $transfer.FileName) 
           
} 
finally 
{ 
# Disconnect, clean up 
$session.Dispose() 
} 

 
catch [Exception] 
{ 
Write-Host $_.Exception.Message 
exit 1 
}

But now I get another error code instead:

Error code: 4
Error message from server: Failure
 
Common reasons for the Error code 4 are:
Renaming a file to a name of already existing file.
Creating a directory that already exists.
Moving a remote file to a different filesystem (HDD).
Uploading a file to a full filesystem (HDD).
Exceeding a user disk quota.
Asking user:
Cannot overwrite remote file '/SomeRemoteFolder'.$$
  • Uploaded.log (12.62 KB, Private file)
Description: The Log file

Reply with quote

Advertisement

You can post new topics in this forum