Connection issue using SFTP and JSch: com.jcraft.jsch.JSchException

ParagJ

When trying to connect to my FTP server using SFTP on port 22, I get following exception.

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
    at com.jcraft.jsch.Util.createSocket(Util.java:349)
    at com.jcraft.jsch.Session.connect(Session.java:215)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at com.mycompany.FTPAdapter.connect(FTPAdapter.java:246)
    at com.mycompany.FTPWriterTask.ftpTransport(FTPWriterTask.java:210)
    at com.mycompany.FTPWriterTask.doExecute(FTPWriterTask.java:183)
    at com.mycompany.AbstractTask.execute(AbstractTask.java:123)
    at com.mycompany.FTPWriterTaskTest.testExecute_testSFTP(FTPWriterTaskTest.java:203)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.jcraft.jsch.Util.createSocket(Util.java:343)
    ... 25 more

I am using following code with JDK7u79 and JSch library to connect using SFTP.

try {
        JSch jsch = new JSch();
        session = jsch.getSession(this.getLoginname(), getHostname(), this.getPortInt());
        Properties config = new Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); 
        session.setPassword(this.getPassword());
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        sftpChannel = (ChannelSftp) channel;
    } catch (Exception e) {
        e.printStackTrace();
        String errMsg = "Could not connect to the SFTP site. Reason: " + e.getMessage();
        throw new IOException(errMsg);
    }

The very strange thing is that I can connect to the server (on port 22) using WinSCP client. So I am curious why it cannot connect using Java code? Am I missing anything in the code? I am using Windows7 and my FTP server is WingFTP on probably Windows Server 2012.

UPDATE:

Regarding one of the comments: My FTP server supports SFTP. Using WinSCP, I can connect to the server using SFTP. Here is the log: Please note, I have masked the IP for security reasons.

. 2016-02-22 13:05:37.552 --------------------------------------------------------------------------
. 2016-02-22 13:05:37.552 WinSCP Version 5.7.6 (Build 5874) (OS 6.3.9600 - Windows Server 2012 R2 Datacenter)
. 2016-02-22 13:05:37.552 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2016-02-22 13:05:37.552 Log level: Normal, Logging passwords
. 2016-02-22 13:05:37.552 Local account: INTERNAL\zuics
. 2016-02-22 13:05:37.552 Working directory: C:\Program Files (x86)\WinSCP
. 2016-02-22 13:05:37.552 Process ID: 2956
. 2016-02-22 13:05:37.552 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" 
. 2016-02-22 13:05:37.552 Time zone: Current: GMT+1, Standard: GMT+1 (W. Europe Standard Time), DST: GMT+2 (W. Europe Daylight Time), DST Start: 3/27/2016, DST End: 10/30/2016
. 2016-02-22 13:05:37.552 Login time: Monday, February 22, 2016 1:05:37 PM
. 2016-02-22 13:05:37.552 --------------------------------------------------------------------------
. 2016-02-22 13:05:37.552 Session name: ICS_Test_SFTP (Site)
. 2016-02-22 13:05:37.552 Host name: xx.xxx.xx.xxx (Port: 22)
. 2016-02-22 13:05:37.552 User name: ICS_Test (Password: PbhDMi912, Key file: No)
. 2016-02-22 13:05:37.552 Tunnel: No
. 2016-02-22 13:05:37.552 Transfer Protocol: SFTP (SCP)
. 2016-02-22 13:05:37.552 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2016-02-22 13:05:37.552 Disable Nagle: No
. 2016-02-22 13:05:37.552 Proxy: none
. 2016-02-22 13:05:37.552 Send buffer: 262144
. 2016-02-22 13:05:37.552 SSH protocol version: 2; Compression: No
. 2016-02-22 13:05:37.552 Bypass authentication: No
. 2016-02-22 13:05:37.552 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2016-02-22 13:05:37.552 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2016-02-22 13:05:37.552 KEX: dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN
. 2016-02-22 13:05:37.552 SSH Bugs: A,A,A,A,A,A,A,A,A,A,A,A
. 2016-02-22 13:05:37.552 Simple channel: Yes
. 2016-02-22 13:05:37.552 Return code variable: Autodetect; Lookup user groups: A
. 2016-02-22 13:05:37.552 Shell: default
. 2016-02-22 13:05:37.552 EOL: 0, UTF: 2
. 2016-02-22 13:05:37.552 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2016-02-22 13:05:37.552 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2016-02-22 13:05:37.552 SFTP Bugs: A,A
. 2016-02-22 13:05:37.552 SFTP Server: default
. 2016-02-22 13:05:37.552 Local directory: C:\Users\zuics\Documents, Remote directory: /, Update: Yes, Cache: Yes
. 2016-02-22 13:05:37.552 Cache directory changes: Yes, Permanent: Yes
. 2016-02-22 13:05:37.552 DST mode: 1
. 2016-02-22 13:05:37.552 --------------------------------------------------------------------------
. 2016-02-22 13:05:37.624 Looking up host "xx.xxx.xx.xxx"
. 2016-02-22 13:05:37.624 Connecting to xx.xxx.xx.xxx port 22
. 2016-02-22 13:05:37.672 Server version: SSH-2.0-WeOnlyDo-WingFTP
. 2016-02-22 13:05:37.672 We believe remote version has SSH-2 rekey bug
. 2016-02-22 13:05:37.672 Using SSH protocol version 2
. 2016-02-22 13:05:37.672 We claim version: SSH-2.0-WinSCP_release_5.7.6
. 2016-02-22 13:05:37.710 Doing Diffie-Hellman group exchange
. 2016-02-22 13:05:37.905 Doing Diffie-Hellman key exchange with hash SHA-1
. 2016-02-22 13:05:37.959 Verifying host key rsa2 0x10001,0xb0ba835d0c1ec6a2 11c75258f75f1433 ff5881c58f62a609 4d7f82a9d7666936 4fdc87ab95940ca3 28fbd48e1d9294fa db00b8d56173420d dff1e9e93827e4c1 64e8b615f670db33 6be0bddfc130b136 4f490f351f64def8 480e8e360f8f0cc8 55b8022b7bdebde3 79ad43b862fd1d3c d896f44dc6561dbb af983f7a78399af7  with fingerprint ssh-rsa 1024 fc:92:54:e6:06:04:1a:97:8c:8c:cb:a7:72:e0:86:52
. 2016-02-22 13:05:37.976 Host key matches cached key
. 2016-02-22 13:05:37.976 Host key fingerprint is:
. 2016-02-22 13:05:37.976 ssh-rsa 1024 fc:92:54:e6:06:04:1a:97:8c:8c:cb:a7:72:e0:86:52
. 2016-02-22 13:05:37.976 Initialised AES-128 CBC client->server encryption
. 2016-02-22 13:05:37.976 Initialised HMAC-SHA1 client->server MAC algorithm
. 2016-02-22 13:05:37.976 Initialised AES-128 CBC server->client encryption
. 2016-02-22 13:05:37.976 Initialised HMAC-SHA1 server->client MAC algorithm
! 2016-02-22 13:05:38.173 Using username "ICS_Test".
. 2016-02-22 13:05:38.206 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2016-02-22 13:05:38.207 Using stored password.
. 2016-02-22 13:05:38.254 Sent password
. 2016-02-22 13:05:38.260 Access granted
. 2016-02-22 13:05:38.260 Opening session as main channel
. 2016-02-22 13:05:38.261 Opened main channel
. 2016-02-22 13:05:38.481 Started a shell/command
. 2016-02-22 13:05:38.488 --------------------------------------------------------------------------
. 2016-02-22 13:05:38.488 Using SFTP protocol.
. 2016-02-22 13:05:38.489 Doing startup conversation with host.
> 2016-02-22 13:05:38.535 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2016-02-22 13:05:38.543 Type: SSH_FXP_VERSION, Size: 5, Number: -1
. 2016-02-22 13:05:38.543 SFTP version 3 negotiated.
. 2016-02-22 13:05:38.543 We believe the server has signed timestamps bug
. 2016-02-22 13:05:38.543 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 string are not mandatory
. 2016-02-22 13:05:38.543 Changing directory to "/".
. 2016-02-22 13:05:38.543 Getting real path for '/'
> 2016-02-22 13:05:38.543 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2016-02-22 13:05:38.552 Type: SSH_FXP_NAME, Size: 23, Number: 16
. 2016-02-22 13:05:38.552 Real path is '/'
. 2016-02-22 13:05:38.552 Trying to open directory "/".
> 2016-02-22 13:05:38.552 Type: SSH_FXP_LSTAT, Size: 10, Number: 263
< 2016-02-22 13:05:38.566 Type: SSH_FXP_ATTRS, Size: 37, Number: 263
. 2016-02-22 13:05:38.566 Getting current directory name.
. 2016-02-22 13:05:38.718 Listing directory "/".
> 2016-02-22 13:05:38.718 Type: SSH_FXP_OPENDIR, Size: 10, Number: 523
< 2016-02-22 13:05:38.721 Type: SSH_FXP_HANDLE, Size: 12, Number: 523
> 2016-02-22 13:05:38.721 Type: SSH_FXP_READDIR, Size: 12, Number: 780
< 2016-02-22 13:05:38.735 Type: SSH_FXP_NAME, Size: 133, Number: 780
> 2016-02-22 13:05:38.735 Type: SSH_FXP_READDIR, Size: 12, Number: 1036
< 2016-02-22 13:05:38.749 Type: SSH_FXP_STATUS, Size: 28, Number: 1036
< 2016-02-22 13:05:38.749 Status code: 1
> 2016-02-22 13:05:38.749 Type: SSH_FXP_CLOSE, Size: 12, Number: 1284
. 2016-02-22 13:05:38.749 Adaptiv 360_v3.pdf;-;577093;2016-02-17T15:09:38.000Z;"user" [0];"group" [0];rwxrwxrwx;1
. 2016-02-22 13:05:38.809 Startup conversation with host finished.
ParagJ

I was doing a mistake in setting the password. It is fixed after entering the correct password. Shouldn't have posted this question.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

com.jcraft.jsch.JSchException: failed to send sftp request

From Dev

com.jcraft.jsch.JSchException: verify: false

From Dev

Jsch connection with private key fails with com.jcraft.jsch.JSchException: Auth fail error

From Dev

Jsch connection with private key fails with com.jcraft.jsch.JSchException: Auth fail error

From Dev

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

From Dev

com.jcraft.jsch.JSchException: Auth fail error

From Dev

com.jcraft.jsch.JSchException: Auth fail error

From Dev

com.jcraft.jsch.JSchException: channel is not opened when opening a channel in jsch

From Dev

com.jcraft.jsch.JSchException: java.io.IOException: Pipe closed

From Dev

How to add TLS feature for SFTP connection using JSch

From Dev

Unknown location in SFTP using JSCH

From Dev

Folder does not exists when changing directory using jcraft.jsch

From Dev

How to tell if an SFTP upload was successful using JSch

From Dev

Multiple file downloads from SFTP using JSch failing with "Request queue: Unknown request issue"

From Dev

How to reput on JSch SFTP?

From Dev

Can open SFTP connection via WinSCP, but cannot with JSch

From Dev

SSH connection using JSCH, UnknownHostException on Android

From Dev

will com.jcraft.jsch.ChannelSftp.get(String src,String dst) over rides the existing file?

From Java

JSch "pipe closed" when using SFTP download InputStream after disconnect

From Dev

How to resolve Java UnknownHostKey, while using JSch SFTP library?

From Dev

JSCH: SFTP. Hangs at session.connect() using the port 21

From Dev

How to download SFTP file by using regular expression in JSch

From Dev

Reading remote file using jsch and sftp - Failure 4

From Dev

JSch sftp channel runs synchronous?

From Dev

Jsch not working with certain sftp servers

From Dev

JSch sftp channel runs synchronous?

From Dev

JSch SFTP transfer changes encoding

From Dev

JAVA JSCH SFTP programming approach

From Dev

Async sftp file transfer with Jsch?

Related Related

  1. 1

    com.jcraft.jsch.JSchException: failed to send sftp request

  2. 2

    com.jcraft.jsch.JSchException: verify: false

  3. 3

    Jsch connection with private key fails with com.jcraft.jsch.JSchException: Auth fail error

  4. 4

    Jsch connection with private key fails with com.jcraft.jsch.JSchException: Auth fail error

  5. 5

    com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

  6. 6

    com.jcraft.jsch.JSchException: Auth fail error

  7. 7

    com.jcraft.jsch.JSchException: Auth fail error

  8. 8

    com.jcraft.jsch.JSchException: channel is not opened when opening a channel in jsch

  9. 9

    com.jcraft.jsch.JSchException: java.io.IOException: Pipe closed

  10. 10

    How to add TLS feature for SFTP connection using JSch

  11. 11

    Unknown location in SFTP using JSCH

  12. 12

    Folder does not exists when changing directory using jcraft.jsch

  13. 13

    How to tell if an SFTP upload was successful using JSch

  14. 14

    Multiple file downloads from SFTP using JSch failing with "Request queue: Unknown request issue"

  15. 15

    How to reput on JSch SFTP?

  16. 16

    Can open SFTP connection via WinSCP, but cannot with JSch

  17. 17

    SSH connection using JSCH, UnknownHostException on Android

  18. 18

    will com.jcraft.jsch.ChannelSftp.get(String src,String dst) over rides the existing file?

  19. 19

    JSch "pipe closed" when using SFTP download InputStream after disconnect

  20. 20

    How to resolve Java UnknownHostKey, while using JSch SFTP library?

  21. 21

    JSCH: SFTP. Hangs at session.connect() using the port 21

  22. 22

    How to download SFTP file by using regular expression in JSch

  23. 23

    Reading remote file using jsch and sftp - Failure 4

  24. 24

    JSch sftp channel runs synchronous?

  25. 25

    Jsch not working with certain sftp servers

  26. 26

    JSch sftp channel runs synchronous?

  27. 27

    JSch SFTP transfer changes encoding

  28. 28

    JAVA JSCH SFTP programming approach

  29. 29

    Async sftp file transfer with Jsch?

HotTag

Archive