B4A Library Android SFTP based on JSch tutorial

JSch is an open source Java implementation of SSH2. It supports many features.

Currently the Basic4android JSch library supports the SFTP protocol which is SSH File Transfer Protocol or Secured File Transfer Protocol.

SFTP is similar to FTP with the difference that the communication is done over a secured channel.

A good tutorial about SSH authentication is available here: SSH Host Key Protection | Symantec Connect Community

When the client first connects to the SSH server he needs to approve the host key (unless it is in the list of approved keys).

SS-2013-03-05_12.08.04.png


The SFtp object raises a PromptYesNo event for this question:
B4X:
Sub sftp1_PromptYesNo (Message As String)
   Dim res As Int = Msgbox2(Message, "", "Yes", "", "No", Null)
   'The next line might be a bit confusing. It is a condition.
   'The value will be True if res equals to DialogResponse.POSITIVE.
   sftp1.SetPromptResult(res = DialogResponse.POSITIVE)
End Sub
The network thread will wait until you call SetPromptResult or after 60 seconds. In the later case the result will be False.

If you want to automatically accept the host key (which should only be done in a secured local network) then you can write:
B4X:
Sub sftp1_PromptYesNo (Message As String)
   sftp1.SetPromptResult(True)
End Sub

Sftp should be declared as a process global variable and initialized in Activity_Create (or Service_Create):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      sftp1.Initialize("sftp1", "username", "password", "example.com", 22)
      sftp1.SetKnownHostsStore(File.DirInternal, "hosts.txt")
   End If
   Activity.LoadLayout("1")
End Sub

Calling SetKnownHostsStore sets a file that will save the known keys. Without it the user will need to approve the key each time.

In Activity_Resume we need to call SFtp.Activity_Resume:
B4X:
Sub Activity_Resume
   sftp1.Activity_Resume
End Sub
The purpose of this call is to redisplay the visible prompt if it was visible when the activity was paused. This is relevant for example when the user changes the orientation while the dialog is visible.

SFtp can also show a message to the user in some cases. This is done with the ShowMessage event:
B4X:
Sub sftp1_ShowMessage (Message As String)
   Msgbox(Message, "")
End Sub

The other methods work in the same way as the FTP object: Android FTP tutorial

The library is attached. The open source project is embedded in the jar file.

V1.30 - Based on the latest version of Jsch (0.1.54).
 

Attachments

  • JSch.zip
    274 KB · Views: 1,170
Last edited:

Kwame Twum

Active Member
Licensed User
Longtime User
It's an FTP server...I tried using the Net Library but got an error (something like: unknown SSL protocol)
Thought this might help.
 

Kwame Twum

Active Member
Licensed User
Longtime User
I posted in a similar thread on the Net Library with FTP. Check the response here
 

AscySoft

Active Member
Licensed User
Longtime User
A small question. My implementation of sftp transfer works pretty well. But when rename command is issued, it takes sometime more than a minute to complete(witch will trigger my tiemeout implementation sub). However if he try to resend that file all goes well (10-12 sec) with small exception when user will try to send that file a third time. Why is that?
My sftp server is a synology NAS station and it works pretty well. My files are small in sizes around 3-5 kb. While the upload file will take only 3 sec, the rename command often gets timed out. Any idea?
I also notice that if user first try to download something... and then upload a file, timeout doesn't happen almost never.
Does this has to do with NAS settings? Or is an issue with sftp.Rename implementation?
 

AscySoft

Active Member
Licensed User
Longtime User
No, because the ideea was to upload a file and to "mark it" for other expecting software at the server side that this file is not yet "ready". Ie: upload a zip archive as a ".zi" and then rename when completed to ".zip". Is there another approach?
 

AscySoft

Active Member
Licensed User
Longtime User
So you are saying I should put a dummy small file "lock" then program my other software to leave alone any file that is pointed to by lock(this should be easy) then delete the lock. I think this should do the trick. Thanks allot.
 

AscySoft

Active Member
Licensed User
Longtime User
After I change the code to send two file (first lock then real one, both with success) and I issued sftp.delete command, my app always get timeouted. Even when I deactivate my timeout, nothing happens. No deletion is made on server side. WHY?
 

AscySoft

Active Member
Licensed User
Longtime User
Wow.. it's exactly like this... I though that if I send a sftp.close command this command will be executet when connection was no longer needed, so after DeleteFile or Rename etc. Thanks for this advice.
In other words, if I leave it open and somebody else (from another device) try to connect using the same credentials will this work?
 

B4JExplorer

Active Member
Licensed User
Longtime User
So, is the jSCH library, the official way of executing sftp commands, in B4J or Basic4Android?
 

Similar Threads

Top