B4A Library [Libary] jkSSH2 - SSH2 communication in B4A

jkSSH2

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.

Summary
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.

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

Example
This example shows how to execute a shell command on the server.
B4X:
'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)
   
   SSH2.initialize("SSH", "hostname", 22)
   SSH2.authenticateWithPassword("username", "password")

   SSH2.execCommand("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)
      Next
   End If
   
   SSH2.close
End Sub

Sub SSH_ConnectionLost (Reason As String)
   Log(Reason)
End Sub
 

Attachments

  • jkSSH2.zip
    233.8 KB · Views: 1,195
Last edited:

hayderOICO

Member
Licensed User
Longtime 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

etc...
 

hayderOICO

Member
Licensed User
Longtime 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:

hayderOICO

Member
Licensed User
Longtime User
HI again.

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

Tyler

Member
Licensed User
Longtime 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:

aidymp

Well-Known Member
Licensed User
Longtime 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?

Thanks

Adrian
 

Tyler

Member
Licensed User
Longtime 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.
 

aidymp

Well-Known Member
Licensed User
Longtime 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.

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

Thanks
 

Tyler

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

B4JExplorer

Active Member
Licensed User
Longtime User
jkSSH2

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.

Summary
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.

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

Example
This example shows how to execute a shell command on the server.
B4X:
'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)
   
   SSH2.initialize("SSH", "hostname", 22)
   SSH2.authenticateWithPassword("username", "password")

   SSH2.execCommand("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)
      Next
   End If
   
   SSH2.close
End Sub

Sub SSH_ConnectionLost (Reason As String)
   Log(Reason)
End Sub

Very helpful, thanks Ribber.
 

Noel

Member
Licensed User
Longtime 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?
 

ludogomez

Member
Licensed User
Longtime User
Hi,

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 ?

Thanks
 

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hi,
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

hAddress="Host1"
hUsername="User1"
hPasswors="Pass1"
SSH_Init
SSH.getFile ("test1.txt",File.DirDefaultExternal ,10)

end sub
Step 2

Sub GetFile

hAddress="Host2"
hUsername="User2"
hPasswors="Pass2"
SSH_Init
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 ?

Thanks.
 

Taha

Member
Licensed User
Longtime 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.

B4X:
Dim tt As String
           DisableStrictMode
           ssh2.initialize("SSH", "IP", 22)
           ssh2.authenticateWithPassword("root", "volumio")
           tt="ls " & QUOTE & "/mnt/USB/1998.Золотое время" & QUOTE
           ssh2.execCommand(tt, 10)
    End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Top