Arun
Arun

Reputation: 3680

SSH Authentication Fail : Java JSCH

My user would need to validate his Private Key against his Git Repository . It is similar to "Test Connection" button in any DB Client tool.

I am using JSCH to do this validation in Java (i just need to connect using SSH and tell that connection is successful). Below is my Java code

public class SSHConnect {


    private String filePath = "c:\\me\\ssh-keys\\config_31_jan";


    public static void main(String... args) {
         new SSHConnect().invoke();
    }

    public void invoke () {
        JSch jSch = new JSch();
        try {

            jSch.addIdentity(filePath);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            Session session = jSch.getSession("MY_USER_NAME","github.my_company.com",22) ;
            session.setConfig(config);
            session.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I get the below exception.

c:\me\ssh-keys\config_31_jan
com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:519)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at com.pcfdev.main.SSHConnect.invoke(SSHConnect.java:32)
    at com.pcfdev.main.SSHConnect.main(SSHConnect.java:18)

I referred all other SO Forums and the solution given was to add the Public key in the corresponding server. I did that and am able to succesfully Authenticate using my SSH Command (as mentioned below). But i couldn't achieve the same using JSCH. Please help

WGC1008Q5B8H2 MINGW64 /c/me/ssh-keys
$ ssh -i config_31_jan -T [email protected]_company.com
Hi ARUNK2/spring-cloudconfig! You've successfully authenticated, but GitHub does not provide shell access.

Upvotes: 2

Views: 2298

Answers (1)

VonC
VonC

Reputation: 1323953

 jSch.getSession("MY_USER_NAME","github.my_company.com",22) ;

This should be instead:

 jSch.getSession("git","github.my_company.com",22) ;

You want to open an ssh connection as git, not as you.
Then your public key on the server side will authenticate you as you.

Upvotes: 3

Related Questions