We are running JSch
from webMethods
server.
After upgrade to new version when we tried to connect to one of our partners it hangs on the connect step.
The proposed solution here hasn't been working. I've received this error :
com.jcraft.jsch.JSchException: Auth fail
I've noticed the server we are trying to connect to is asking for user and password before connection is done:
[sagtest@indigo sftpTest]$ /opt/sag/95/sitcore/jvm/jvm/bin/java -jar
SftpConnect.jar
Password has been set
Timeout has been set
Kerberos username [sagtest]:
Kerberos password for sagtest:
Connected
Channel opened
Connected
Session: iui-bpjobs01.itservices.lan:22:bp-xfer-int:1738666864
The Kerberos
user and password shouldn't be there at all.
After small investigation I've found this page:
Skipping Kerberos authentication prompts with JSch
After setting PreferredAuthentications
to other values - all has been working fine. The code that I've added:
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
Here is the full example that I've got - maybe it will help someone to debug your issue:
public static void main (String[] args){
String serverport = "22";
String serverhost = "XXXX";
String username = "YYYY";
String password = "ZZZZZ";
int timeout = 5000;
try{
int port = 22;
String sessionkey = null;
if(serverport != null && !serverport.trim().equals(""))
port = Integer.parseInt(serverport);
JSch jsch=new JSch();
Session session=jsch.getSession(username, serverhost, port);
jsch.setKnownHosts(System.getProperty("user.home")+"/.ssh/known_hosts");
session.setPassword(password);
System.out.println("Password has been set");
if(timeout > 0){
session.setTimeout(timeout);
System.out.println("Timeout has been set");
}
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
System.out.println("Added PreferredAuthentications");
session.connect();
System.out.println("Connected");
Channel channel = session.openChannel("sftp");
System.out.println("Channel opened");
channel.connect();
System.out.println("Connected");
if (channel.isConnected()){
sessionkey = serverhost + ":" + serverport + ":" + username + ":" + Thread.currentThread().hashCode();
}
System.out.println("Session: "+sessionkey);
}catch(Exception e){
e.printStackTrace();
}
}