Discussion:
[JSch-users] Session.connect(): Connection refused
A Yang
2013-12-12 22:21:54 UTC
Permalink
Hi,

I'm looking for some advice on how I would troubleshoot the following exception:

The code in question runs properly on an Ubuntu development workstation, and I am able to connect via the sftp cilent on the deployment environment (Red Hat). However, executing the code on Red Hat results in the 'Connection refused' error. I can also telnet to the remote SFTP server on port 22 from the deployment box.

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused
at com.jcraft.jsch.Util.createSocket(Util.java:389)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.xyz.sftp.SFTPService.connect(SFTPService.java:107)
at com.xyz.sftp.SFTPService.init(SFTPService.java:46)
at com.xyz.sftp.SFTPUtility.init(SFTPUtility.java:44)
at com.xyz.sftp.SFTPUtility.run(SFTPUtility.java:50)
at com.xyz.sftp.SFTPUtility.execute(SFTPUtility.java:67)
at com.xyz.sftp.SFTPUtility.main(SFTPUtility.java:44)
Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Connection refused
at com.xyz.sftp.SFTPService.init(SFTPService.java:50)
at com.xyz.sftp.SFTPUtility.init(SFTPUtility.java:44)
at com.xyz.sftp.SFTPUtility.run(SFTPUtility.java:50)
at com.xyz.sftp.SFTPUtility.execute(SFTPUtility.java:67)
at com.xyz.sftp.SFTPUtility.main(SFTPUtility.java:44)
Caused by: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused
at com.jcraft.jsch.Util.createSocket(Util.java:389)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.xyz.sftp.SFTPService.connect(SFTPService.java:107)
at com.xyz.sftp.SFTPService.init(SFTPService.java:46)
... 4 more

As part of the init process, I load a known_hosts file and a private key from file:

public void init() {
try {
loadKnownHosts();

byte[] privateKey = readPrivateKeyFromFile(_keyFileName);
addIdentity(_userId, privateKey, _passPhrase.getBytes());
connect(_server, _userId);
}
catch(Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

protected void connect(String server, String userId) throws JSchException {
_session = _jsch.getSession(userId, server, 22);
_session.setDaemonThread(true);
_session.setServerAliveInterval(5000); // keep alive messages
_session.connect(); // ***** EXCEPTION OCCURS ON THIS LINE
Channel channel = _session.openChannel("sftp");
_sftpChannel = (ChannelSftp) channel;
_sftpChannel.connect();
}

I can confirm that the private key is loaded properly, but I can't really tell if my known_hosts file is causing the problem. Could an invalid known_hosts file result in a 'Connection refused' error?

Thanks for your time

Loading...