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.

Updates:
1.31 - New Rmdir method - deletes empty folders.
1.30 - Based on the latest version of Jsch (0.1.54).
 

Attachments

  • JSch.zip
    274.1 KB · Views: 220
Last edited:

AscySoft

Active Member
Licensed User
Longtime User
nicely done

Well, and finally him does the job...again, I mean, what do you want more...??
:sign0060::sign0060:
A big thanks, now I feel more secure!
And maybe one day(or two) somebody will develop another fine library supporting FTPS (see Apache.network.common)
So, to you Erel a BIG THANKS!
:wav:
 

AscySoft

Active Member
Licensed User
Longtime User

lorebarita

Member
Licensed User
Longtime User
Synchro feature and searching for files in SFTP

Hi, the library is very nice and would like to know if it is possible to add these features:
- synchro feature where one can set a file or folder and automatically it syncrhonizes android2server or server2android
- possibility of searching for a file if its not known where it is located within subfolders when trying to download (the root where the file is, is known but not subfolders name)
- possibility to know the datetime of the file on the server

Thanks
Lorenzo
 

boredsilly

Member
Licensed User
Longtime User
Command execution possible?

Is it possible to execute a remote command as it is with standard ftp as I'd like to chmod the files on the server once uploaded.
 

boredsilly

Member
Licensed User
Longtime User
I'd like to be able to rename the file extension from .tmp to .new when upload is complete (a means for the server to know the file is completely uploaded) also I may have to chmod (change permissions) on the file.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Any chance there is an example project to view?
Thanks,
Rusty
 

Kwame Twum

Active Member
Licensed User
Longtime User
Not working for me

When I try to upload a file to the server or download a file from it, I get a Timed Out error after a while.

Codes
B4X:
Sub Activity_Create(FirstTime As Boolean)
    bu.Initialize("buu")
    bu.Text = "Upload"
    Activity.AddView(bu,20%x,45%y,60%x,10%y)
    tfp.Initialize("ttp", "username", "password","xxxxxxxxxx.com", 22) 'Credentials are however correct - tested with desktop ftp client
End Sub

B4X:
Sub buu_Click
Dim dial As FileDialog
    dial.FilePath= File.Combine(File.DirRootExternal, "/")
    If dial.Show("Select a file", "Lego!", "Cancel","",Null)=-1 Then
        Log("Attempting to upload " & dial.FilePath &  "/" & dial.ChosenName)
        tfp.UploadFile(dial.FilePath & "/" , dial.ChosenName, "palztech/palztechgroup.com/wwwroot/" & dial.ChosenName)
        tfp.Close
    End If
End Sub

Logs after running and selecting a file to upload
B4X:
Attempting to upload /mnt/sdcard/bluetooth/00001.vcf
palztech/palztechgroup.com/wwwroot/00001.vcf, Success=false
java.net.SocketTimeoutException: Connection timed out

Am I doing somthing wrong?
 
Top