I am really stamped and running out of ideas as of why my ssh connection from one Unix server to another Unix server, this is my java code to establish ssh connection between to unix server:
import com.jcraft.jsch.*;
import java.util.Properties;
public class ScpFiles {
public static void main(String[] arg){
// TODO code application logic here
JSch jsch = null;
Session session = null;
Channel channel = null;
String userName = "a_user";
String host = "unixserver1";
String pass = "";
String phrase = "";
String knownHostFile = "/home/a_user/.ssh/known_hosts";
String rsa = "/home/a_user/.ssh/id_rsa";
try{
jsch = new JSch();
session = jsch.getSession(userName, host, 22);
session.setPassword(pass);
jsch.addIdentity(rsa, phrase.getBytes());
jsch.setKnownHosts(knownHostFile);
//Properties prop = new Properties();
//prop.put("StrictHostKeyChecking", "no");
//session.setConfig(prop);
session.connect();
channel.connect();
String command = "an unix command";
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
}catch (Exception e){
e.printStackTrace();
}finally {
channel.disconnect();
session.disconnect();
}
}
}
as you can see, it is pretty straight forward, but somehow it always ended with this error:
com.jcraft.jsch.JSchException: Auth cancel
at com.jcraft.jsch.Session.connect(Session.java:463)
at com.jcraft.jsch.Session.connect(Session.java:158)
at ScpFiles.main(ScpFiles.java:43)
Exception in thread "main" java.lang.NullPointerException at ScpFiles.main(ScpFiles.java:53)
I even tried to compromise the security for test purpose by setting the StrictHostKeyChecking to no, but i still can't ssh to the remote unix server.
i was googling this error a few days, but sounds like the answer posted there doesnt resolve my issue, would someone in here please shed me a light on what went wrong.
Thanks heaps.
com.jcraft.jsch.JSchException: Auth fail at com.jcraft.jsch.Session.connect(Session.java:464) at com.jcraft.jsch.Session.connect(Session.java:158) at ScpFiles.main(ScpFiles.java:44)
– Andie Oct 06 '16 at 07:31where i am pretty sure the user name and password are correct as the ssh command worked perfectly. I will try to get the log file and see what exactly went wrong.