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)