1

How to connect to a remote unix host from java without passing username and password.

The below code is what I am using

   package testing;     

    import java.io.InputStream;

    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;

    public class Test {

        public static void main(String[] args) {

            execute();
        }

            public static void execute() {
                    try {
                        JSch jsch = new JSch();

                        String user = "xxxx";
                        String host = "xxxxxx";
                        int port = 22;
                        String privateKey = "xxxxxxxxx";

                        jsch.addIdentity(privateKey);
                        System.out.println("identity added ");

                        Session session = jsch.getSession("nudayaku","cloudgw-dev-01" );
                        System.out.println("session created.");
                        session.setConfig("PreferredAuthentications", "publickey");
                        java.util.Properties config = new java.util.Properties();    
                        config.put("StrictHostKeyChecking", "no");      
                        session.setConfig(config);  
                        session.connect();
                        System.out.println("session connected.....");

                    } catch (Exception e) {
                        System.err.println(e);
                    }
                }
    }

Please find the below error.

stack trace error

com.jcraft.jsch.Session.connect(Session.java:519)
     com.jcraft.jsch.Session.connect(Session.java:183)
     testing.Test.execute(Test.java:39)
     testing.Test.main(Test.java:17)
8
  • So you have code. What is your actual problem? Is it working? How is it not working? Do you not know how to use it? Commented May 10, 2020 at 12:50
  • Hi Stephen,I am using this code but getting the below error Commented May 10, 2020 at 12:58
  • identity added session created. com.jcraft.jsch.JSchException: Auth fail Commented May 10, 2020 at 12:58
  • Add the details to the Question please. Commented May 10, 2020 at 12:59
  • updated the same in question Commented May 10, 2020 at 13:00

1 Answer 1

1

Based on the evidence so far, the most likely causes are (IMO):

  1. You are using the wrong host, and the account doesn't exist on that host.
  2. You are using the wrong user name for the remote account
  3. You are using a private key that doesn't match one of the public keys recorded for the remote account
  4. The remote server is not configured to accept PublicKey auth.
  5. The remote account's ~/.ssh directory is not configured correctly. A common problem is for either the ~/.ssh directory or the ~/.ssh/authorized_keys file to be too open.

I think that we can eliminate problems with the private key file. I think that would give you a different exception.


If none of the above is the cause, then you need to turn on JSch logging at debug level to get more insights as to what is going wroing. Unfortunately, JSch doesn't use a logging framework by default, but there is an example in the JSch examples page that gives code that you can borrow:

Finally, if client-side logging doesn't help, see if you can (temporarily) turn on debugging for sshd on the remote server side. (Be aware that sshd is a security / privacy concern. Sensitive information about all SSH connections will be logged.)


This is not the problem, but the following code looks a bit strange:

session.setConfig("PreferredAuthentications", "publickey");
java.util.Properties config = new java.util.Properties();    
config.put("StrictHostKeyChecking", "no");      
session.setConfig(config);  

Why don't you write the last 3 statements as follows?

session.setConfig("StrictHostKeyChecking", "no");
Sign up to request clarification or add additional context in comments.

Comments

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.