com.jcraft.jsch.JSchException: Auth fail error

余生长醉 提交于 2019-12-24 04:10:30

问题


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.


回答1:


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

Is that a PuTTY-format keyfile? Ie, 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.




回答2:


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



来源:https://stackoverflow.com/questions/24071188/com-jcraft-jsch-jschexception-auth-fail-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!