Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.jcraft.jsch.JSchException: Auth fail error

Tags:

jsch

Trying to connect to a host using ssh key auth. Below is my code:

package com.mkyong.common;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * 
 */
public class UserAuthPubKey {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try {
            JSch jsch = new JSch();

            String user = "XXXXXXXX";
            String host = "XXXXXXXX.XXXXXXX.com";
            int port = 22;
            String privateKey = "~/.ssh/WF_OPENSSH.ppk";
            String passphrase = "XXXXXXXXXXX";

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

            Session session = jsch.getSession(user, host, port);
            System.out.println("session created.");

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

            session.connect();
            System.out.println("session connected.....");

            Channel channel = session.openChannel("sftp");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect();
            System.out.println("shell channel connected....");

            ChannelSftp c = (ChannelSftp) channel;

//            String fileName = "test.txt";
//            c.put(fileName, "./in/");
//            c.exit();
//            System.out.println("done");

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

what change should i make here. On debugging the error seems to occur at session.connect(); statement. I am using a private key and a passphrase to connect.

like image 293
Kriz Avatar asked Nov 26 '25 20:11

Kriz


1 Answers

String privateKey = "~/.ssh/WF_OPENSSH.ppk";

Is that a PuTTY-format keyfile? Was it generated from puttygen, the PuTTY key generation utility? Jsch only reads OpenSSH-format key files, not PuTTY-format files.

You can use puttygen to convert the key to OpenSSH format if you want to use that key. See this question.

like image 200
Kenster Avatar answered Nov 28 '25 15:11

Kenster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!