javasftpsshj

Java SSHJ library and enabling logging


Below is a example section of my java 1.8 program. It appears to be failing when trying to authenticate. It goes through a number of authentication methods and then declares its run out. I would like to see debug information from within the sshj library to help me determine what's failing :- username, key exchange or something else. I am familiar with log4j and I can put logging statements within my code, but I can't find an example (simple to follow) which shows me how to hook up log4j to sfl4j and then tell sshj to use the logger.

'''

        SSHClient sshClient = new SSHClient();

        try
        {
            
            String username = "testuser";
            File privateKey = new File("/mykeys/keyname");
            KeyProvider keys;
            
            sshClient.addHostKeyVerifier(new PromiscuousVerifier());
            
            keys = sshClient.loadKeys(privateKey.getPath());
            
            sshClient.connect("1.2.3.4", 22);
            
            sshClient.authPublickey(username, keys);

            SFTPClient sftpClient = sshClient.newSFTPClient();
            
            sftpClient.put("./send/file1.xml", "file1.xml");
            

            sshClient.close();
        }

        catch (UserAuthException e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }
        catch (TransportException e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }

'''


Solution

  • Adding

            <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.6</version>
        </dependency>
    
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    

    to pom.xml

    and creating a log4j.proerties files did the trick for me:

    # Define the root logger with appender file  
    log = ssh-test.log
    log4j.rootLogger = DEBUG, FILE  
    
    # Define the file appender  
    log4j.appender.FILE=org.apache.log4j.FileAppender  
    log4j.appender.FILE.File=${log}/log.out  
    
    # Define the layout for file appender  
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout  
    log4j.appender.FILE.layout.conversionPattern=%m%n