B4A Library [Libary] jkSSH2 - SSH2 communication in B4A

Discussion in 'Additional libraries, classes and official updates' started by ribber, Jul 31, 2012.

  1. ribber

    ribber Member Licensed User


    This libary is based on Ganymed SSH-2 libary. An open source java implementation of the SSH2 protocol. The licence is included in the zip file.

    This libary currently offers the following SSH2 features:
    • Several authorization methods (None/Username and Password/Key)
    • Execute shell commands on the server.
    • Create local port forwardings.
    • Download files with the scp command.
    • Upload files with the scp command.

    1. Download and unzip the attached zip file.
    2. Copy jkSSH2.jar and jkSSH2.xml to the additional libaries folder.

    This example shows how to execute a shell command on the server.
    'Activity module
    Sub Process_Globals
    'These global variables will be declared once when the application starts.
       'These variables can be accessed from all modules.

    End Sub

    Sub Globals
    'These global variables will be redeclared each time the activity is created.
       'These variables can only be accessed from this module.
       Dim SSH2 As jkSSH2
    End Sub

    Sub Activity_Create(FirstTime As Boolean)

    "uname -a && date && uptime && who"10)

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub SSH_CmdExecuted (Success As Boolean, Result As List, TaskId As Int)
    Log (TaskId)
    If Success Then
    For i=0 To Result.Size-1
    Log(Result.Get(i) & CRLF)
    End If
    End Sub

    Sub SSH_ConnectionLost (Reason As String)
    End Sub

    Attached Files:

    Last edited: Aug 1, 2012
    manda, calsdn, inakigarm and 7 others like this.
  2. vb1992

    vb1992 Well-Known Member Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for sharing this library. I've created a wiki user for you. If possible please add this library to the libraries list: Libraries - Basic4android Wiki
    luke2012 likes this.
  4. hayderOICO

    hayderOICO Member Licensed User

    Hi all. another noob question!

    I want the SSH connection to remain alive in its own thread so I don't need to reconnect every time a button is pressed.

    sub Globals
    Dim SSH2 As jkSSH2
    Dim Button1, Button2 As Button

    where can I put the code in red? so I don't have to initialise the SSH connection for each button press?
    SSH2.initialize("SSH", "hostname", 22)
    SSH2.authenticateWithPassword("username", "password")

    Sub Button1_Click
    SSH2.execCommand("ls -l")
    End Sub

    Sub Button2_Click
    SSH2.execCommand("cd /")
    End Sub

  5. hayderOICO

    hayderOICO Member Licensed User

    hi again.

    I've got the jkSSH2 running in a separate service and then from the main thread executing commands. using the

    SSH2.execCommand("uname -a && date && uptime && who", 10)

    BUT.... and here's the issue. do I need to have the command number for it? the original ganymed library doesn't need it as I can see from its example java code (known host.java) so why does the B4A implementation need it?

    I am asking because users would send the same command multiple times in one session AND there are about 200+ buttons in total!

    even though I did this to enable the same command to be sent multiple times:

    Sub Button1_Click
    Dim x As Int
    x = Rnd(1,100)
    SSHservice.SSH2.execCommand(" fim test1.jpg", x)
    End Sub

    I am not sure if the SSHexec can handle the 200+ separate commands...

    Sub Button1000_Click
    Dim x As Int
    x = Rnd(100000,100100)
    SSHservice.SSH2.execCommand(" fim test1000.jpg", x)
    End Sub
    Last edited: Jun 27, 2013
  6. hayderOICO

    hayderOICO Member Licensed User

    HI again.

    Is there a possibility of the "startShell" command from GanymedSSH2 being implemented in the JKSSH2 ?
  7. Tyler

    Tyler Member Licensed User

    Is there any way to receive the terminal output? Not just send the commands? I'd LOVE to be able to display some output in Label or EditText views.

    My reason for asking is I run game servers and I'm making an app to administrate them. I use Linux screen sessions to run the console for each game. I've figured out how to send commands to the screen session through jkssh2 but I really want to be able to see the console output so I can be sure the commands worked correctly and see any information the commands output. Also, I'm not clear on the reason for needing a taskid and which taskids correlate to which tasks.

    Thanks for this awesome, simple library. Much appreciated!
    Last edited: Sep 15, 2014
  8. aidymp

    aidymp Active Member Licensed User

    Hi, Ive got this library working, but I too would like to see the output of the terminal, m going to send a large file to a remote device, and would like some idea of progress and success! is it possible or not?


  9. Tyler

    Tyler Member Licensed User

    Well, you can get very limited terminal output through sub cmd_executed(result as string), it appears to show the output from the last command entered. For a"progress report", if I'm not mistaken, FTP(net lib) has that option and might be a better option for you.
  10. aidymp

    aidymp Active Member Licensed User

    I will have a look into the cmd_executed, The FTP command in netlib does that allow connectino via ssh?

  11. Tyler

    Tyler Member Licensed User

    Unless otherwise set up, ftp and ssh are essentially the exact same thing. They're both run through port 22 by default.
  12. B4JExplorer

    B4JExplorer Active Member Licensed User

    Very helpful, thanks Ribber.
    luke2012 likes this.
  13. Noel

    Noel Member Licensed User

    I'm getting the following error when testing the above code (with my SSH data)

    "An error has occurred in sub: android.os.NetworkOnMainThreadException"

    Anyone here who might know how to fix this?
  14. Noel

    Noel Member Licensed User

  15. ludogomez

    ludogomez Member Licensed User


    I want to use your library to talk with severals servers on the same time and I want to differentiate all the responses from servers.
    I try that but all the response are mix between the servers

    Is it possible ?

  16. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    First of All, thanks for the Lib. It is really helpfull.
    Now, a little question :)
    I Open the connection and download a file.
    All works well.
    then I want to connect to another host and to open another file.
    Either closing the connection firstm or initializing it again with another host, I always get an error downloading the second file.
    Code below

    Step 1

    Sub SSH_Init
    SSH.initialize("SSH", hAddress, 22)
    SSH.authenticateWithPassword(hUsername, hPassword)
    End Sub

    Sub GetFile

    SSH.getFile ("test1.txt",File.DirDefaultExternal ,10)

    end sub
    Step 2

    Sub GetFile

    SSH.getFile ("test2.txt",File.DirDefaultExternal ,10)

    End sub​

    If I call SSH.Close before Step2 I get and error downloading. If not, I get the same error.
    How can I download from several hosts, in sequence ?

  17. Taha

    Taha Member Licensed User

    Excellent lib, thanks! However, can't understand non-English characters. The following code executes but returns empty result list. The same happens with European characters.

    Please, help.

    Dim tt As String
    "ls " & QUOTE & "/mnt/USB/1998.Золотое время" & QUOTE
    End Sub
  18. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    Hi All

    Is this library supported anymore ?


  19. DonManfred

    DonManfred Expert Licensed User

    The Author was last seen in 2015
  20. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    Yeah, I saw that, any alternative's available ?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice