Resolution: Could not parse response code. Server reply:ssh-2.0-openssh_5.3

Source: Internet
Author: User

[ABSTRACT: Transact: Org.apache.commons.net.MalformedServerReplyException:Could not parse response code. Server reply:ssh-2.0-openssh_5.3 when applying org.apache.commons.net.ftp.]

Resolution: Org.apache.commons.net.MalformedServerReplyException:Could not parse response code. Server reply:ssh-2.0-openssh_5.3

When using org.apache.commons.net.ftp.FTPClient via protocol SSH2 to make SFTP connection times as above error, because it does not support this way of connection (using Ftpsclient SSL is also not possible).

Example code:

Package Com.jerval.test;import Java.io.ioexception;import Java.util.arraylist;import java.util.arrays;import Java.util.list;import Org.apache.commons.net.ftp.ftpclient;import Org.apache.commons.net.ftp.ftpreply;import Org.slf4j.logger;import Org.slf4j.loggerfactory;public class Ftpfilelist {private static final Logger LOG = loggerfact    Ory.getlogger (Ftpfilelist.class);    public static void Main (string[] args) {printlist ();  } private static void Printlist () {list<string> List = listfilenames ("Fca-vm-rds-prod1", "Applog", "AAA",        "/webapp/myrds1/lib");        for (String filename:list) {System.out.println (fileName);        }} private static List<string> Listfilenames (string host, string user, string password, string dir) {        list<string> list = new arraylist<string> ();        FtpClient ftpclient = new FtpClient ();            try {ftpclient.connect (host, 22);   int reply = Ftpclient.getreplycode ();         if (ftpreply.ispositivecompletion (Reply)) {ftpclient.login (user, password);                Ftpclient.changeworkingdirectory (dir);                string[] names = Ftpclient.listnames (dir);            List.addall (arrays.aslist (names));        }} catch (IOException e) {log.error ("error!", e);        } finally {close (ftpclient);    } return list;            } private static void Close (FtpClient ftpclient) {if (null! = FtpClient && ftpclient.isconnected ()) { try {ftpclient.logout ();//exit FTP server} catch (IOException e) {e.prints                Tacktrace (); System.out.println ("Exit FTP server Exception!                ");            System.out.println (E.getmessage ()); } finally {try {ftpclient.disconnect ();//Close the FTP server connection SYSTEM.OUT.PR                Intln ("Exit and close the connection to the FTP server");                 } catch (IOException e) {   E.printstacktrace (); System.out.println ("Close the FTP server connection exception!")                    ");                System.out.println (E.getmessage ()); }            }        }    }}

Error message:

3    [main] ERROR com.jerval.test.FtpFileList  -error!org.apache.commons.net.malformedserverreplyexception: Could not parse response code. Server reply:ssh-2.0-openssh_5.3at org.apache.commons.net.ftp.ftp.__getreply (ftp.java:333) at Org.apache.commons.net.ftp.ftp.__getreply (ftp.java:290) at Org.apache.commons.net.ftp.ftp._connectaction_ ( ftp.java:396) at Org.apache.commons.net.ftp.ftpclient._connectaction_ (ftpclient.java:796) at Org.apache.commons.net.SocketClient.connect (socketclient.java:172) at Org.apache.commons.net.SocketClient.connect (socketclient.java:192) at Com.jerval.test.FtpFileList.listFileNames ( FTPFILELIST.JAVA:32) at Com.jerval.test.FtpFileList.printList (ftpfilelist.java:22) at Com.jerval.test.FtpFileList.main (ftpfilelist.java:18) Org.apache.commons.net.MalformedServerReplyException: Could not parse response code. Server Reply:protocol mismatch.

Workaround:

The

uses the SSH workaround provided by Com.jcraft.jsch.JSch. The code is as follows:

Package Org.jerval.test.ftp;import Java.util.arraylist;import Java.util.list;import java.util.properties;import Java.util.vector;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Com.jcraft.jsch.Channel;import Com.jcraft.jsch.channelsftp;import Com.jcraft.jsch.channelsftp.lsentry;import Com.jcraft.jsch.JSch;import Com.jcraft.jsch.session;public class Ftpsfilelist {private static final Logger LOG = Loggerfactory.getlogger (ftpsfilel    Ist.class); public static void Main (string[] args) {listfilenames ("fca-vm-rds-prod1.xxx.org", "a", "Applog", "xxx", "/webapp/my    Rds1/lib ");  private static list<string> Listfilenames (string host, int port, string username, final string password, string        dir) {list<string> List = new arraylist<string> ();        Channelsftp sftp = null;        Channel channel = NULL;        Session sshsession = null;            try {Jsch Jsch = new Jsch ();          Jsch.getsession (username, host, port);  Sshsession = Jsch.getsession (username, host, port);            Sshsession.setpassword (password);            Properties Sshconfig = new properties ();            Sshconfig.put ("stricthostkeychecking", "no");            Sshsession.setconfig (Sshconfig);            Sshsession.connect ();            Log.debug ("Session connected!");            Channel = Sshsession.openchannel ("sftp");            Channel.connect ();            Log.debug ("Channel connected!");            SFTP = (channelsftp) channel;            vector<?> vector = Sftp.ls (dir);                for (Object item:vector) {lsentry entry = (lsentry) item;            System.out.println (Entry.getfilename ());        }} catch (Exception e) {e.printstacktrace ();            } finally {Closechannel (SFTP);            Closechannel (channel);        CloseSession (sshsession);    } return list; } private static void Closechannel (Channel channel) {if (channel! = NULL{if (channel.isconnected ()) {channel.disconnect (); }}} private static void CloseSession (Session session) {if (session! = NULL) {if (Sessio            N.isconnected ()) {session.disconnect (); }        }    }}

Maven dependencies:

    <dependency>      <groupId>com.jcraft</groupId>      <artifactId>jsch</artifactId>      <version>0.1.49</version>    </dependency>

Resolution: Could not parse response code. Server reply:ssh-2.0-openssh_5.3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.