EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Looking for SFTP Code Samples

Posted: 10/23/2007 16:09:19
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Does anyone have a quick code sample for two issues I am trying to figure out?

First I am trying to use the OpenFile() method to send a file through SFTP since I am sending from Windows to a Unix server and the Unix server has protocol version 3.

Second I am looking for code to send a key to log in instead of having to hard code the login name and password.

If anyone has samples it would be greatly appreciated, if not I will look at the knowledgebase some more and muddle my way through.
Posted: 10/24/2007 00:18:01
by Eugene Mayevski (Team)

The examples are located in <SecureBlackbox>\Samples folder. They include all you need

Sincerely yours
Eugene Mayevski
Posted: 10/24/2007 10:26:28
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Thank you Eugene, I found code for the key, but there was nothing in the three code samples for the OpenFile() Method.

I have a text file on the server that is 11 characters for each line. I tried the original version of the code, but it was transferring in Binary and was adding extra characters at the end of each line.

I need to remove the extra characters and since the Unix server is SFTP protocol 3, I can't use the ASCII attribute, I need to use the OpenFile() method.

So far I have this....

Client.OpenFile(parLocalName, 64, )

Where I am stuck is on the attributes portion and any additional code that needs to run before I upload the file.

Again thank you for the initial help.
Posted: 10/24/2007 13:10:50
by Eugene Mayevski (Team)

In brief you need to use

Handle = OpenFile
while not (EndOfOurFile)
WriteFile(Handle, DataBlock)

As you didn't specify anything about the language / edition, I can't give you more detailed instructions.

Sincerely yours
Eugene Mayevski
Posted: 10/24/2007 15:44:35
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Thank you Eugune, I am using Visual Studio .NET 2005 using SDK 2.0. I am trying to code this in VB .NET.

Here is a sample of the code I am trying to make in the app.

Imports SBSftpCommon
Imports SBSimpleSftp
Imports SBSSHKeyStorage

Module ConsoleSftpClient

Dim Client As TElSimpleSFTPClient
Dim strRemoteName As String = "/"
Dim strLocalName As String
Dim KeyStorage As TElSSHMemoryKeyStorage

Sub Upload()
Client.UploadFile(strLocalName, strRemoteName, TSBSFTPFileTransferMode.ftmOverwrite)
End Sub

Sub SFTPMissFile()

Dim strPrivKey As String
strPrivKey = "something"
KeyStorage = New TElSSHMemoryKeyStorage

Client = New TElSimpleSFTPClient
Client.KeyStorage = KeyStorage
Client.Address = ""
Client.Port = "22"
'Client.Username = "something"
'Client.Password = "something else"
strRemoteName = "/serverlocation/" & strFileName
strLocalName = filMiss

Dim key As New TElSSHKey
Dim privateKeyAdded As Boolean = False
If key.LoadPrivateKey(strPrivKey, "password") = 0 Then
Client.AuthenticationTypes = Client.AuthenticationTypes Or SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY
privateKeyAdded = True
End If

If Not privateKeyAdded Then
Client.AuthenticationTypes = Client.AuthenticationTypes And Not SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY
End If

Client.OpenFile(strLocalName, 64, (what do I put here?))

End Try

Catch ex As Exception
End Try

End Sub

End Module
Posted: 10/25/2007 00:42:00
by Eugene Mayevski (Team)

Did you actually read my last post? Your code is not correct.

Sincerely yours
Eugene Mayevski
Posted: 10/25/2007 02:25:43
by Eugene Mayevski (Team)

As for the attributes, you can pass Nothing. This is always the simpliest way - if you don't know what to put, try putting 0 (zero) or Nothing.

Sincerely yours
Eugene Mayevski
Posted: 10/25/2007 10:40:52
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Eugene, thank you for the information and I did read your code, I just wanted to post what version I was using and to give my code sample.

I found some of the issues, I am trying to open the file on the client side, I am using the application to send the file from a Windows location to a Unix Server.

Since the OpenFile method is from the Server side am I out of luck?

Posted: 10/25/2007 13:32:57
by Eugene Mayevski (Team)

Your code is not correct, unfortunately. There are just 3 functions - OpenFile, WriteFile and CloseHandle. Please check documentation for the parameters to use. It's simple.

Sincerely yours
Eugene Mayevski
Posted: 10/30/2007 09:24:29
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Eugene I am not able to get it to work. I know I am doing something wrong, but I can't find what I am doing wrong.

I have tried the following line:

Handle = Client.OpenFile(strLocalName, 64, Nothing)

But it errors out every time, because the LocalName is on the Windows server and cannot find the file.

Second, there is only a Write() command not a WriteFile() command.



Topic viewed 15243 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS business operates as a division of /n software, inc. For more information, please read the announcement.

Got it!