3

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.

Tuna
  • 2,937
  • 4
  • 37
  • 61
Kriz
  • 357
  • 4
  • 7
  • 15

2 Answers2

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

Kenster
  • 23,465
  • 21
  • 80
  • 106
1

Get the lastest version of JSch. The old version shows Auth Fail for no reason

GarethViljoen
  • 119
  • 1
  • 3