I'm trying to create a JSch tunnel to ssh to an IP which is a passwordless setup to run a script. When I manually do an ssh to that IP, I don't need a password. card.ipAddress gives the IP address to ssh to and it is a passwordless setup. I have log.info (print) statements so I could know where exactly I was getting an exception. remoteShellScripthas the path to the script.
com.jcraft.jsch.Session jschSession = null;
try {
log.info("Entering try block of JSch session");
JSch jsch = new JSch();
jschSession = jsch.getSession(USERNAME, card.ipAddress, REMOTE_PORT);
log.info("after getSession");
// not recommend, uses jsch.setKnownHosts
jschSession.setConfig("StrictHostKeyChecking", "no");
log.info("after setting Config");
// authenticate using password
//jschSession.setPassword(PASSWORD);
// connect timeout session
jschSession.connect(); //This is where I get the Exception
log.info("after connecting to jschSession");
ChannelExec channelExec = (ChannelExec) jschSession.openChannel("exec");
log.info("Channel is open");
// run a shell script
channelExec.setCommand("sh " + remoteShellScript + "\"" + cn_ip + "\" " + duration);
log.info("after running script");
// display errors to System.err
channelExec.setErrStream(System.err);
log.info("after setErr");
//InputStream in = channelExec.getInputStream();
// 5 seconds timeout channel
channelExec.connect();
log.info("after connect");
} catch (Exception e) {
log.error("Catching exception", e);
e.printStackTrace();
} finally {
log.info("Disconnecting session from Finally block");
if (jschSession != null) {
jschSession.disconnect();
}
Exception:
Catching exception: com.jcraft.jsch.JSchException: Auth fail