SFTP exception types

+1 vote
asked Sep 7, 2016 by Prabhakar (610 points)

1)In SFTP, what kind of exception is thrown when the API Login(username, password, privatekey) fails? NetworkSessionExceptionStatus is very generic and I am looking for specific type like 'Authentication Failed'?. Is there a way of achieving it?
2)In SFTP, how do I verify that the file was uploaded without data loss or corruption?

Applies to: Rebex SFTP

2 Answers

0 votes
answered Sep 7, 2016 by Tomas Knopp (58,890 points)
edited Sep 8, 2016 by Tomas Knopp

1) In case the password/ key authentication request was not accepted by the server, the SftpException is thrown by the Sftp.Login method. The Status will be SftpExceptionStatus.OperationFailure in this case.

2) The SSH protocol itself prevents the packets from being "mistreated", so if Rebex Sftp client finishes the file upload without an error being thrown, the file should be uploaded just fine.

But if you really want to make sure the file integrity is ok, the good way to do so would be to compute the hash of the remote file.

The next release of Rebex SFTP will allow you to compute the remote file checksums (if the server supports it) - if you would like to receive a beta version supporting it, let us know! Update - here is the link to the trial beta version.

Please note that for instance OpenSSH does not support this SFTP extension. But even with SFTP servers that don't support it, it's possible to compute the remote checksum via the SshChannel.RequestExec method. See this forum post for more details and sample code how to remotely compute a hash via a remote command on the SFTP server.

commented Sep 8, 2016 by Prabhakar (610 points)
Can you please provide us the Beta version.  I couldn't see sftp having RemoteExec method, though.
commented Sep 8, 2016 by Tomas Knopp (58,890 points)
Sorry, it is not Sftp.RemoteExec method, it is SshChannel.RequestExec method. I just corrected it. As to the beta with support for Sftp.GetRemoteChecksum method - I am sharing the link to download in my other answer. Please give it a try and let me know if it works for you.
0 votes
answered Sep 8, 2016 by Tomas Knopp (58,890 points)
edited Sep 8, 2016 by Tomas Knopp

Here is the promised Rebex SFTP beta version with the Sftp.GetRemoteChecksum method. You can retrieve the remote checksum like this:

Sftp sftp = new Sftp();
// connect, login, upload file, ...

// compute the remote checksum (SHA256)
string checkSumSha256 = sftp.GetRemoteChecksum(Rebex.ChecksumAlgorithm.SHA256, file);

If you want to check that a file has been uploaded correctly, just check that that the checksum of the uploaded file actually matches the expected checksum computed on the source file.

Please note that only some SFTP servers support the 'check-file' extension that this method utilizes (for instance Cerberus SFTP server does support it). If your server does not support the extension, you can still use the SshChannel.RequestExec method to execute the remote command to calculate the checksum of the file at the server (see a code example to issue a remote exec command).

commented Sep 8, 2016 by Prabhakar (610 points)
Thanks a lot. Will test and update you. Do you know if Windriver SFTP server supports this feature?
commented Sep 8, 2016 by Tomas Knopp (58,890 points)
I am not familiar with Windriver SFTP server, sorry. Just give it a try and see.