2

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.

1 Answer 1

3
 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.

Sign up to request clarification or add additional context in comments.

6 Comments

One more question. Is there a way to add privateKey as a string directly ? rather mentioning it as FilePath
Adding the private directly hard-coded in the code would be a bad practice, so I hope not.
I am writing a App that will accept Private-Key from a HTML texbox and then validate against git and upon successful validation given him back some auto-configruations JSON File. So i will have to inject the value from the textbox to my JSCH code
Can you inject it in a temporary file, whose path you would then inject or use in your code?
@Arun Or at least, if you don't have write access to the user TEMP folder, write it in memory: stackoverflow.com/a/22307964/6309 or in a byte stream: stackoverflow.com/a/17595282/6309
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.