I'm trying to connect to EC2 server using Jsch SSH client with RSA KeyPair, But it is giving me auth fail error. I have checked my configuration, even I'm able to connect to EC2 using terminal with same KeyPair.
package org.example;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;
import java.util.Scanner;
import java.util.UUID;
import java.util.regex.Pattern;
public class Ssh {
private Session session = null;
private final HostInfo host;
private BufferedReader reader;
private BufferedReader error;
private ChannelShell channel;
private BufferedWriter writer;
public Ssh(HostInfo host) {
this.host = host;
}
public void setup() throws Exception {
JSch jSch = new JSch();
jSch.addIdentity("aws-ssh.pem");
session = jSch.getSession(host.getUsername(), host.getHost(), host.getPort());
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = (ChannelShell) session.openChannel("shell");
channel.connect();
InputStream in = channel.getInputStream();
InputStream err = channel.getExtInputStream();
OutputStream out = channel.getOutputStream();
reader = new BufferedReader(new InputStreamReader(in));
writer = new BufferedWriter(new OutputStreamWriter(out));
error = new BufferedReader(new InputStreamReader(err));
}
}
Error:
Exception in thread "main" 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 org.example.Ssh.setup(Ssh.java:30)
at org.example.Main.main(Main.java:12)
I have try password authentication mode that works, but I want to authenticate via SSH key pair(.pem).