B4J Library jSSHClient Library

This SSH Client connect library is still in testing. It depends on the jsch-0.1.51.jar. Extract all files to your additional libs folder. Designed to work on B4J **Does not work on B4A**

Please advise of any errors, issues.

Good Luck.:)

Sorry

I removed the library due to a code error on the String info. Will upload the corrected version shortly.
 
Last edited:

giga

Well-Known Member
Licensed User
Longtime User
I have corrected the string error in this library, I am able to see the attempts in the logs.

So I know it hits the server and authenticates.

However a major problem I am having is getting a terminal to open in order to send/receive commands.

Any suggestions would be great.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi giga, do you have this client working yet ??

I've to establish a SSH tunnel between my PC and a MYSQL server and enable port forwarding to query the MYSQL database (for now I've to use SSHTunnelClient from Delight Softwarer via jShell succesfully) but I'd like to use an integrated solution if possible

Thanks
 

giga

Well-Known Member
Licensed User
Longtime User
I have the library working in the sense of connection. I connect and receive a response from the server, but sending commands after connected still is an issue. The connection establishes then shortly after I get a response for the server(com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 2 Too many authentication failures for TestUser)
You are more than welcome to try it. Any ideas always appreciated.

It depends on the jsch-0.1.51.jar. Extract all files to your additional libs folder. Designed to work on B4J **Does not work on B4A**

The new library is in post #15
 
Last edited:

inakigarm

Well-Known Member
Licensed User
Longtime User
Tested. I can access via SSH to my server but that's only I can do

Does this library only have the connect command? (commands, port forwarding)
 

giga

Well-Known Member
Licensed User
Longtime User
Yes, at this point the library makes a connection to the server through SSH. As I mentioned in post #3 & #6 the problem is there is no terminal like window to send/receive commands that I can find. Any ideas on how to fix this is welcomed.

This is my problem area in the library. I have no way to display session.openChannel("shell");

B4X:
  JSch shell = new JSch();  
        // get a new session    
        Session session = shell.getSession(user, host, port);  
 
        // set user password and connect to a channel  
        session.setUserInfo(new SSHUserInfo(password));  
        session.connect();  
        Channel channel = session.openChannel("shell");  
        channel.connect();
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Have you seen this Thread ?? https://www.b4x.com/android/forum/threads/how-to-create-b4j-library.49959/#post-312795

It's simplest to use Javaobject (or inline java: tried but a little more complicated) as @rwblinn shows in the post.

I've succesfully coded a portforwarding for MYSQL after a SSH connection: (setPortForwardingL method)

B4X:
    Dim joSch As JavaObject
    'Java: JSch js = new JSch();
    joSch.InitializeNewInstance("com.jcraft.jsch.JSch", Null)
    'Java: Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    Dim joS As JavaObject
    joS = joSch.RunMethod("getSession", Array(user, host, port))
    'Java: s.setPassword("mypassword");
    joS.RunMethod("setPassword", Array(pw))
    'Java: Properties config = new Properties();
    Dim joP As JavaObject
    joP = joP.InitializeNewInstance("java.util.Properties", Null)
    'Java: config.put("StrictHostKeyChecking", "no");
    joP.RunMethod("setProperty", Array("StrictHostKeyChecking", "no"))
    'Java: s.setConfig(config);
    joS.RunMethod("setConfig", Array(joP))
    'Java: s.connect();
    joS.RunMethod("connect", Null)
    Log("Connected to Host...")
    joS.RunMethod("setPortForwardingL",Array(3306,"127.0.0.1",3306))'Array(tunnelLocalPort,tunnelRemoteHost,tunnelRemotePort);
    Log("PortFWD to MYSQL...")
 

giga

Well-Known Member
Licensed User
Longtime User
Here is the updated SSH Client library with the ability to send commands and read server responses.
Extract all files to your additional libs folder. Designed to work on B4J **Does not work on B4A**

There are a few items to note using this library.
1. It depends on the jsch-0.1.51.jar.
2. It uses ("StrictHostKeyChecking", "no") so some would consider it to not be as secure of a connection.
3. The way I use it is by outputting the stream to a file ("C:\Results\","SSHResults.txt") and reading it with a TextReader (response may be slower?)
4. Commands are sent singularly and the session closes after the server response to the command.
5. Example command ls -la\r\n

Let me know if there are any issues. Good luck, Hope it's helpful
 

Attachments

  • JSSHClient.zip
    244.7 KB · Views: 519
Top