B4J Library SSHJ - ssh, scp, sftp for Java

Discussion in 'B4J Libraries & Classes' started by mindful, Jan 21, 2018.

  1. mindful

    mindful Active Member Licensed User

    This is a wrapper for: https://github.com/hierynomus/sshj

    Dependencies:
    #AdditionalJar: slf4j-api-1.7.25
    #AdditionalJar: sshj-0.23.0
    #AdditionalJar: eddsa-0.2.0
    #AdditionalJar: bcprov-jdk15on-159

    You can download this libraries from:
    slf4j-api-1.7.25: https://mvnrepository.com/artifact/org.slf4j/slf4j-api
    sshj-0.23.0: https://mvnrepository.com/artifact/com.hierynomus/sshj
    eddsa-0.2.0: https://mvnrepository.com/artifact/net.i2p.crypto/eddsa
    (B4J) bcprov-jdk15on-159: https://www.bouncycastle.org/latest_releases.html
    (B4A) prov-1.58.0.0.jar: https://mvnrepository.com/artifact/com.madgag.spongycastle/prov

    *NOTE1* I tested this only with B4J, it should work for Android too!
    *NOTE2* Also when running in Release you should set #MergeLibraries: False because of the bouncy castle dependency (bcprov-jdk15on-159) which is a signed jar, and when running with #MergeLibraries: True that jar is decompiled and compiled in your jar and therefore losses its signing.
    *NOTE3* For Android you need to download spongy castle instead of bouncy castle. You can download it from https://mvnrepository.com/artifact/com.madgag.spongycastle/prov. I don't know if NOTE2 applies in this case.

    First steps:
    Code:
    Dim ssh As SSHJ
        ssh.Initialize(
    "ssh")
        
    '...... or ......
        ssh.Initialize2("ssh"15' where 15 is the KeepAliveInterval - usefull for portforward and future sftp.
    As connecting through ssh to a server wants to mean that is secure you have to add the server hostkey:
    Code:
    ssh.LoadKnownHosts 'loads the known_hosts file from some default locations.
    'or
    ssh.LoadKnownHosts2("/tmp/known_hosts")
    'or
    ssh.AddHostKeyVerifier("SHA1:2Fo8c/96zv32xc8GZWbOGYOlRak=")
    'or finnaly if you really trust the server and do not want to verify it's key
    ssh.AddHostKeyPromiscuousVerifier
    Next step is the authentication. You can supply multiple authentication methods:
    Code:
    ssh.AddAuthPassword("youruserpass")
    'or/and
    ssh.AddAuthPublicKey("/location/to/my/key""null if not encrypted")
    'or/and
    ssh.AddAuthPublicKey2("key-key-key-key...""empty string if public key is in private key string""null if not encrypted")
    Finnaly now we connect:
    Code:
    ssh.Connect("hostname/ip"22"yourusername")
    All the execution methods (read details in ide) - Exec, Shell, SCPUpload, SCPDownload, LocalPortforwarder, RemotePortForwarder and all methods of SFTPClient - are async and work with Wait For feature of B4X.

    Like always if you encounter problems i will do my best to help, but I'm no expert!
     

    Attached Files:

    Last edited: May 9, 2018
    OliverA, JackKirk, miker2069 and 6 others like this.
  2. mindful

    mindful Active Member Licensed User

    updated the library to version 1.31 (see first post). It's complete now as it includes a full featured SFTPClient.

    Best regards,
     
    OliverA and Erel like this.
  3. mindful

    mindful Active Member Licensed User

    Can someone test if this works with android too ?

    It appears that on library github in the wiki page they have another configuration than the one provided with the library. In the current configuration I don't think it is necessary to include the eddsa-0.2.0.jar o_O
     
  4. Terence Agius

    Terence Agius Member Licensed User

    Hello
    When I try to use this under android, I get unsupported class file version 52.0
     
  5. mindful

    mindful Active Member Licensed User

    This is because it's compiled with java 8 ... I will try to compile with java 7 - maybe it works ... and i will report back with a new java 7 version if the compilation will be successful.
     
  6. Terence Agius

    Terence Agius Member Licensed User

    Thank you. Much appreciated.
     
  7. mindful

    mindful Active Member Licensed User

    @Terence Agius a new version compiled with java 7 has been added to the first post. Please test and let me know if it works with Android ...
     
  8. Terence Agius

    Terence Agius Member Licensed User

    well, it did compile
    but it didn't work ... crashed
    Will have to debug
     
  9. mindful

    mindful Active Member Licensed User

    Please post some code and logs. As is mentioned in the 3rd post in the sshj github there is a page that explains how to make it work on android that is different from the current implementation of the library itself. so without seeing any code or logs I do not know for what to look.
     
  10. Terence Agius

    Terence Agius Member Licensed User

    Tried
    Even with Android specific jar
    Crashes


    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: B4A Example
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    #AdditionalJar: slf4j-api-1.7.25
    '#AdditionalJar: sshj-0.23.0
    #AdditionalJar: gradle-wrapper
    '#AdditionalJar: eddsa-0.2.0
    #AdditionalJar: bcpg-jdk15on-1.58.0.0

    #BridgeLogger: True
    '#MergeLibraries: False

    'Activity module
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Dim ssh As SSHJ
    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)
      
       
    If FirstTime = True Then
            
    Log("Diable")
               DisableStrictMode
       
    End If
        ssh.Initialize2(
    "ssh"15)
        ssh.LoadKnownHosts
        ssh.AddHostKeyPromiscuousVerifier
        ssh.AddAuthPassword(
    "password")
        ssh.Connect(
    "192.168.10.18"22"root" )
        ssh.Exec(
    "uname -a && date && uptime && who"10)
        

    End Sub

    Sub Activity_Resume

    End Sub

    Sub DisableStrictMode
       
    Dim jo As JavaObject
       jo.InitializeStatic(
    "android.os.Build.VERSION")
       
    If jo.GetField("SDK_INT") > 9 Then
         
    Dim policy As JavaObject
         policy = policy.InitializeNewInstance(
    "android.os.StrictMode.ThreadPolicy.Builder"Null)
         policy = policy.RunMethodJO(
    "permitAll"Null).RunMethodJO("build"Null)
         
    Dim sm As JavaObject
         sm.InitializeStatic(
    "android.os.StrictMode").RunMethod("setThreadPolicy"Array(policy))
       
    End If
    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
      
        ssh.close
    End Sub

    Sub SSH_ConnectionLost (Reason As String)
        
    Log(Reason)
     
  11. mindful

    mindful Active Member Licensed User

    '#AdditionalJar: sshj-0.23.0 -> You most definetly need this so uncomment it
    '#AdditionalJar: eddsa-0.2.0 -> I am not sure if this is needed or not - you have to test

    #MergeLibraries: False -> In b4j if you set False it doesn't pack the libraries inside the generated jar and they need to be copied manually near the jar.


    You didn't post any logs ... so I have no ideea what to recommend :(

    I will adventure in B4A soon and will be able to test this ...
     
  12. Terence Agius

    Terence Agius Member Licensed User

    #AdditionalJar: gradle-wrapper is sshj-0.23

    and there are no logs that is why I didn't post any...the Log pane remains empty

    #MergeLibraries does not work that is why its in a remark
     
  13. ShaneG30

    ShaneG30 Well-Known Member Licensed User

    I'm testing this library out and I can't connect to my server. The error message is:
    Code:
    java.io.IOException: Could not load known_hosts
     
  14. mindful

    mindful Active Member Licensed User

    @ShaneG30 ssh.LoadKnownHosts 'loads the known_hosts file from some default locations.

    As connecting through ssh to a server wants to mean that is secure you have to add the server hostkey:
    Code:
    ssh.LoadKnownHosts 'loads the known_hosts file from some default locations.
    'or
    ssh.LoadKnownHosts2("/tmp/known_hosts")
    'or
    ssh.AddHostKeyVerifier("SHA1:2Fo8c/96zv32xc8GZWbOGYOlRak=")
    'or finnaly if you really trust the server and do not want to verify it's key
    ssh.AddHostKeyPromiscuousVerifier
    You should try using AddHostKeyPromiscuousVerifier ..
     
    ShaneG30 likes this.
  15. ShaneG30

    ShaneG30 Well-Known Member Licensed User

    @mindful - I got it working and am connected to my server and it works wonderfully.
     
    mindful likes this.
  16. mindful

    mindful Active Member Licensed User

    @ShaneG30 are you using it with B4A or B4J ?
     
  17. ShaneG30

    ShaneG30 Well-Known Member Licensed User

    Right now just B4J.
     
  18. Terence Agius

    Terence Agius Member Licensed User

    @mindful I was wondering if you have any updates regarding Android as I have not managed to go forward yet
     
  19. imbault

    imbault Well-Known Member Licensed User

    Hi, could you send a sampled code using SFTP, please?

    Thanks a lot

    Patrick
     
  20. mindful

    mindful Active Member Licensed User

    Sure
    Code:
    Dim ssh As SSHJ
        ssh.Initialize2(
    "ssh"15)
        ssh.AddHostKeyPromiscuousVerifier
        ssh.AddAuthPassword(
    "youruserpass")
        ssh.Connect(
    "hostname/ip"22"yourusername")
       
        
    Dim sftp As SSHJSFTPClient = ssh.GetSFTPClient
       
        
    sftp.Upload("your-file-path""server-file-path"False)
    SFTP can be used for many thing besides file transfer between host and remote. You can find details in the ide at each method. ide.png
     
    imbault likes 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