commons-net is compatible with ssh-2.0 protocol
Asked Answered
P

4

9

i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3

i have followed this article for create my connection, and with official examples i've controlled article.

my java code here:

  private void connect(String host, String user, String pwd) {
        try{
        ftp = new FTPSClient(false);
        //ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;

        ftp.connect(host,22);//error is here
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftp.login(user, pwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

I do not understand where I went wrong.

Phylis answered 19/12, 2013 at 9:2 Comment(0)
T
21

The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library

    JSch jsch = new JSch();
    Session session = jsch.getSession( user, host, port );
    session.setConfig( "PreferredAuthentications", "password" );
    session.setPassword( pass );
    session.connect( FTP_TIMEOUT );
    Channel channel = session.openChannel( "sftp" );
    ChannelSftp sftp = ( ChannelSftp ) channel;
    sftp.connect( FTP_TIMEOUT );
Threw answered 19/12, 2013 at 9:13 Comment(0)
I
4

SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).

Intromission answered 19/12, 2013 at 9:10 Comment(0)
T
2
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {

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

    String user = "username";
    String host = "hostadress";
    int port = 22;
    String pass = "password";
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(pass);
    session.connect();
    System.out.println("Connection established.");
    System.out.println("Creating SFTP Channel.");
}

try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.

Transmontane answered 14/6, 2018 at 13:12 Comment(0)
S
0

Change the port to 21 to connect to FTP server instead of SFTP(port 22)

Sturm answered 8/11, 2021 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.