B4J Library jSSHClient Library

Discussion in 'B4J Libraries & Classes' started by giga, Feb 2, 2015.

  1. giga

    giga Well-Known Member Licensed User

    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: Feb 5, 2015
  2. Shay

    Shay Well-Known Member Licensed User

    Thanks !!
     
  3. giga

    giga Well-Known Member Licensed 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.
     
  4. Shay

    Shay Well-Known Member Licensed User

    Sorry I have no idea
     
  5. inakigarm

    inakigarm Well-Known Member Licensed 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
     
  6. giga

    giga Well-Known Member Licensed 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: Apr 21, 2016
    inakigarm likes this.
  7. inakigarm

    inakigarm Well-Known Member Licensed User

    Thanks giga, I'll try on Monday
     
  8. inakigarm

    inakigarm Well-Known Member Licensed 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)
     
  9. giga

    giga Well-Known Member Licensed 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");

    Code:
    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();
     
  10. inakigarm

    inakigarm Well-Known Member Licensed User

    Have you seen this Thread ?? http://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)

    Code:
    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...")
     
  11. giga

    giga Well-Known Member Licensed User

    I will check this one and see, The main issue is not having a terminal (shell) SSH.
     
  12. Shay

    Shay Well-Known Member Licensed User

    Any update on SSH connection library on B4j?
     
  13. giga

    giga Well-Known Member Licensed User

    Got side tracked with other projects but haven't given up.
     
  14. giga

    giga Well-Known Member Licensed User

    Here is the progress on the SSH Library. Corrected issue with the connection and now able to get server welcome response and able to send command.
    Having issue with "End Line" freezing window. Hope to get it uploaded in the next few days.
     
    Shay likes this.
  15. giga

    giga Well-Known Member Licensed 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
     

    Attached Files:

    inakigarm and Erel like this.
Loading...
  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