Domanda

I am connecting to FTP server via sftp(JSCH).

Evertime i connect to the FTP server using the port 21, it always hangs at session.connect().

It does not throw any exception. But when i use other ports. It works and it throws exception.

Is there any way i could catch the error?

Here is a sample of my code.

public static void main(String[] args) throws SftpException {

    JSch jsch = new JSch();

    try {

        Session session = jsch.getSession("username", "host", 21);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("password");
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp channelSftp = (ChannelSftp) channel;
        session.disconnect();
        channelSftp.disconnect();

    } catch (JSchException e) {
        log("Cannot make connection to FTP server ");
        e.printStackTrace();

    }

}
È stato utile?

Soluzione 2

Could it be that port 22 is the default port for SFTP? And a FTP server running on port 21 won't know how to negotiate the conversation for secure FTP. Basically, SFTP is FTP over SSH.

EDITED: The issue is, it is waiting indefinitely for the negotiation to complete. It is a Mexican stand-off with neither side giving up. Call session.setTimeout() before session.connect(), or call session.connect(timeout), with some suitable value (3-5 seconds). I believe the timeout is in milliseconds.

Altri suggerimenti

I had a similar problem but with the correct port. session.connect() just hangs and does not return anything. While other programs can successfully connect.

The reason was that the host provides more authentication methods, and we must point Jsch to use only password.

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        config.put("PreferredAuthentications", "password");
        session.setConfig(config);

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();
    }
}

In my case, only updating JSch to latest version (currently 1.50) fixed the issue, seems there was an incompatibility in key/auth handshake with older versions of JSch in combination with latest OpenSSH version 7.1.x

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top