VB.Net - Cannot Open Session

Advertisement

kelvintan
Joined:
Posts:
1
Location:
Singapore

VB.Net - Cannot Open Session

Hi,

I have been trying to write a method to connect to the server using the local machine private key file to the sFTP server. I can connect successfully using the WinSCP GUI.

Following are my codes. During the debug mode, it always stop at the session.open line and there isn't any useful error message given.

  Try
            Dim sessionOptions As New SessionOptions

            With sessionOptions
                .Protocol = Protocol.Sftp
                .HostName = "xxx.xxx.xxx.xxx"
                .UserName = "xxxxx"
                .PortNumber = "22"
                .SshHostKeyFingerprint = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                .SshPrivateKeyPath = "x:\xxxx\xxxx.ppk"
            End With

            Using Session As Session = New Session
                Session.DisableVersionCheck = True
                Session.SessionLogPath = "D:\sftplog.log"

                ' Connect
                Session.Open(SessionOptions)

                Dim transferOptions As New TransferOptions
                transferOptions.TransferMode = TransferMode.Binary

                Dim transferResult As TransferOperationResult
                transferResult = Session.PutFiles("D:\Outbox\*", "/upload/xxxx/", False, transferOptions)

                ' Throw on any error
                transferResult.Check()

                ' Print results
                Dim transfer As TransferEventArgs
                For Each transfer In transferResult.Transfers
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName)
                Next
            End Using

            Return 0
        Catch e As Exception
            Console.WriteLine("Error: {0}", e)
            Return 1
  End Try

Any help or tips would be greatly appreciated.

Thank you.

Reply with quote

Advertisement

Bozo
Joined:
Posts:
3
Location:
Nowhere

Hi,

I am new to this forum, and was wondering if this has been resolved? I am having the same problem and have been looks for some time to resolve this. Below is my code. I tried to get a session log, but it never gets connected so there is not one. This error on the open statement. This is written in VB .NET 2010 Express.

One more thing, I wrote a VB Console app to execute the WinSCP that used a script file and work worked perfectly. The only issue I had with this method was lack of visibility and control of what may happen in an error situation. So I know all Of the parameters and Public key work.

[code]
Dim sftpOptions As New SessionOptions

With sftpOptions
.Protocol = Protocol.Sftp
.HostName = "xxx.xxx.xxx.xxx"
.UserName = "UserName"
.SshPrivateKeyPath = "C:\FTPFolder\Code\Test.ppk"
.PortNumber = 443
End With

Using sftp As Session = New Session
sftp.SessionLogPath = "C:\FTPFolder\Code\sftplog.log"
sftp.ExecutablePath = "C:\FTPFolder\Code\WinSCP.exe"
sftp.Open(sftpOptions)
[/code]




Any help would be greatly appreciated.

Thanks

ErrorMessage.JPG

Reply with quote

Bozo
Joined:
Posts:
3
Location:
Nowhere

Thank you for the response, but I cannot seem to find the property Hostkey within Sessionoptions. :(
I am really trying to not have to use SshHostKeyFingerprint as this application will be used by multiple businesses and it would be much easier to handle the key by filename instead of Fingerprint. I have included a jpg showing where I am looking for the Hostkey property.

Thank you

Hostkey.JPG

Reply with quote

Advertisement

Bozo
Joined:
Posts:
3
Location:
Nowhere

I was finally able to resolve my issues.

Thank you very much, this is a great product.

[code]
With (sftpOptions)
.Protocol = Protocol.Sftp
.HostName = "xxxxxxxxx.com"
.UserName = "yyyyy"
'.Password = ""
.SshPrivateKeyPath = "C:\FTPFolder\Code\zzzzzzz.ppk"
.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
.PortNumber = 22
End With
[/code]

Reply with quote

Ghostwolf
Joined:
Posts:
2
Location:
Jeffersonville, IN

Please, how did you finally resolve your issues?

I'm having the same problem trying to connect to a server requiring both password and private key authentication.

My SessionOptions are:
  .Protocol = Protocol.Sftp
  .PortNumber = 22
  .HostName = [HostName]
  .UserName = [UserName]
  .Password = [Password]
  .SshPrivateKeyPath = [PathToKeyFile]
  .SshHostKeyFingerprint = "ssh-rsa 1023 xx:..."
Although I've specified a location for a logfile, one isn't being created.

I can connect to the server using WinSCP's GUI interface, but my program process fails at Session.Open with a "Disconnected: No supported authentication methods available (server sent: publickey)" error message.

I'd sure appreciate some guidance on this.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

Ghostwolf wrote:

Although I've specified a location for a logfile, one isn't being created.
How did you specify a log file?

I can connect to the server using WinSCP's GUI interface, but my program process fails at Session.Open with a "Disconnected: No supported authentication methods available (server sent: publickey)" error message.
The "server sent: publickey" means your server support private key authentication only, no password authentication. Are you sure you are using password authentication? Maybe you confuse it for a private key passphrase. See https://winscp.net/eng/docs/faq_passphrase
If that does not help, can you at least attach a log file from GUI?

Reply with quote

Ghostwolf
Joined:
Posts:
2
Location:
Jeffersonville, IN

GUI session log file is attached. Naturally, I've replaced anything I could readily identify as sensitive with [xxx].

The server requires both private key and password for authentication.

I've tried specifying the Session.XmlLogPath two ways:
- full Path\Name: the Session.Open results in the above-mentioned error, but no log file;
- Path only: results in "timeout waiting for WinSCP to respond" error.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

I've tried specifying the Session.XmlLogPath two ways:
- full Path\Name: the Session.Open results in the above-mentioned error, but no log file;
- Path only: results in "timeout waiting for WinSCP to respond" error.
I need a session log, use Session.SessionLogPath.

Reply with quote

doujiangyoutiao
Joined:
Posts:
2
Location:
SHA

I also got this problem , please help.

Below is the log



. 2014-06-24 15:22:18.562 Host key fingerprint is:
. 2014-06-24 15:22:18.562 ssh-rsa 2048 97:c9:38:5e:21:6b:cf:cc:0a:d0:74:8f:bb:93:3b:f7
. 2014-06-24 15:22:18.562 Initialised AES-256 SDCTR client->server encryption
. 2014-06-24 15:22:18.562 Initialised HMAC-SHA1 client->server MAC algorithm
. 2014-06-24 15:22:18.562 Initialised AES-256 SDCTR server->client encryption
. 2014-06-24 15:22:18.562 Initialised HMAC-SHA1 server->client MAC algorithm
. 2014-06-24 15:22:19.177 Reading private key file "C:\Users\dll\Desktop\lenton.ppk"
! 2014-06-24 15:22:19.177 Using username "lenton".
. 2014-06-24 15:22:19.460 Offered public key
. 2014-06-24 15:22:19.790 Offer of public key accepted
! 2014-06-24 15:22:19.790 Authenticating with public key "imported-openssh-key"
. 2014-06-24 15:22:19.790 Prompt (3, SSH key passphrase, , Passphrase for key "imported-openssh-key": )
. 2014-06-24 15:22:19.791 Disconnected: Unable to authenticate

Below is the code
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxxxx",
UserName = "xxxx",
SshPrivateKeyPath = @"C:\Users\dll\Desktop\lenton.ppk",
SshHostKeyFingerprint = "ssh-rsa 2048 xxxx"
//Password = "UjBsA68E"
};

using (Session session = new Session())
{
// Connect
session.SessionLogPath = @"C:\Users\dll\Desktop\sftpLog.txt";
session.Open(sessionOptions);

// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;

TransferOperationResult transferResult;
transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);

// Throw on any error
transferResult.Check();

// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}

Reply with quote

doujiangyoutiao
Joined:
Posts:
2
Location:
SHA

solved now.

now share to the others who got this problem.
there are two things to do
1.run the pageant.exe with the ppk file and password.
2.run the winSCP with administrator permission.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

Re: solved now.

doujiangyoutiao wrote:

now share to the others who got this problem.
there are two things to do
1.run the pageant.exe with the ppk file and password.
2.run the winSCP with administrator permission.
Or read the FAQ I referred to earlier:
https://winscp.net/eng/docs/faq_passphrase
The next version (beta to be released in few weeks) will support providing passphrase:
https://winscp.net/tracker/816

Reply with quote

Advertisement

dibyendur
Joined:
Posts:
1
Location:
Philadelphia, USA

I see that a solution has been devised to automate the functionality of authentication using SSH keys having passphrase.

I'm using below format command line in Windows batch (*.bat) scripts:
"C:\Program Files\winscp432\WinSCP.com" <.. Profile name saved in WinSCP client ..> /console /passive /script=<.. FTP Script path & name ..>

The Profile mentioned in the command above has the .ppk SSH key saved, & prompts for the key passphrase. How can I automatically pass the SSH key's passphrase when I'm executing my batch (*.bat) scripts?

Any assistance would be very helpful. Thanks in advance.

Reply with quote

Advertisement

You can post new topics in this forum