解决:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3
[摘要:办理:org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3 当应用org.apache.commons.net.ftp.]
解决:org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3
当使用org.apache.commons.net.ftp.FTPClient通过协议SSH2进行SFTP连接时报如上错误,原因是它不支持这种方式的连接(使用FTPSClient的SSL也是不行的)。
示例代码:
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 = LoggerFactory.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();// 退出FTP服务器
} catch (IOException e) {
e.printStackTrace();
System.out.println("退出FTP服务器异常!");
System.out.println(e.getMessage());
} finally {
try {
ftpClient.disconnect();// 关闭FTP服务器的连接
System.out.println("退出并关闭FTP服务器的连接");
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭FTP服务器的连接异常!");
System.out.println(e.getMessage());
}
}
}
}
}
错误信息:
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.3
at 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.
解决方法:
使用com.jcraft.jsch.JSch提供的SSH解决方法。代码如下:
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(FtpsFileList.class); public static void main(String[] args) {
listFileNames("fca-vm-rds-prod1.xxx.org", 22, "applog", "xxx", "/webapp/myrds1/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 (session.isConnected()) {
session.disconnect();
}
}
}
}
Maven依赖:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>
解决:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3的更多相关文章
- 解决报错Error response from daemon: Get https://10.0.0.110/v2/: dial tcp 10.0.0.110:443: connect: connection refused
修改 #https不需要验证,否则要加上以下配置# 意思就是非安全仓库,加上重启就OK了! vim /lib/systemd/system/docker.service --insecure-regi ...
- 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法
java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...
- 记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx
踩坑经历 因为项目需要去对接别的接口,使用URLConnection POST请求https接口,发送json数组时遇到java.io.IOException: Server returned HTT ...
- java.io.IOException: Server returned HTTP response code: 411 for URL
今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...
- Java Server returned HTTP response code: 401
今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...
- 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”;Syntax error on token "Invalid Character";Server returned HTTP response code: 503 for URL;
元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”:复制的代码有中文空格 Syntax error on token "Invalid Character&qu ...
- retrying failed action with response code: 403 错误解决
[2019-06-10T06:52:51,610][INFO ][logstash.outputs.elasticsearch] retrying failed action with respons ...
- Jmeter压测报错:Non HTTP response code: java.net.ConnectExceptionexception的解决办法
前一段时间进行jmeter压测时,一直报错,查看了下日志才发现报了一堆Non HTTP response code: java.net.ConnectExceptionexception,直接jmet ...
- Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"
2015-11-16 10:39:17.235 PullDemo[338:60b] Application windows are expected to have a root view contr ...
随机推荐
- css布局2
居中 常用居中 elemP{ text-align: center; } elelmP elemC{ display: inline-block; } elemP{ display: table; m ...
- 8、显示程序占用内存多少.txt
方法一: 要加单元 PsAPI procedure TForm1.tmr1Timer(Sender: TObject); begin edt1.Text:= format('memory use: % ...
- bash脚本中的普通数组和关联数组
1. 普通数组 bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0. ...
- iOS Xcode behaviors个人常用Debug配置
- java中 ==与equals 有什么区别?
1.==既可以比较基本类型变量,又可比较引用类型变量,而equals只能比较引用类型变量: 2.equals方法支持重写,如果未重写equals方法,则比较引用变量时与==都是比较变量所指向的对象地址 ...
- zookeeper启动错误 transaction type: 2 error: KeeperErrorCode = NoNode for /hbase
hbase伪分布式,与zookeeper同一台机器的时候,运行一段时间,启动zookeeper的时候,日志中有如下错误,导致无法启动zookeeper java.io.IOException: Fai ...
- HTML与HTML5笔记
doctype作用 1.告诉浏览器使用什么样的html或者xhtml规范来解析html文档 2.影响浏览器的渲染模式. 浏览器解析css的两种渲染模式:标准模式strict mode和怪异模式quir ...
- jenkins 的 ProcessTreeKiller----无法启动子进程的解决办法
参考: http://alanland.iteye.com/blog/2047244 http://scmbob.org/start-process-in-jenkins.html java -Dhu ...
- Spring MVC Rest 支持CORS
新建cors filter文件, package cn.ac.iscas.pebble.ufe.tools; import java.io.IOException; import javax.serv ...
- 隐式调用 Intent 大全, 很全
http://ming-fanglin.iteye.com/blog/1061312 //调用浏览器 Uri uri = Uri.parse(""); Intent it = n ...