解决: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 ...
随机推荐
- 记录java基础的学习过程
标识符(类名:变量.属性.方法名: ) 组成:类名开头不能是数字,只能有字母数字_$组成. 命名规范: 类名每一个单词首字母大写(HelloWorld大驼峰法则), 方法名 属性名 变量名首字母小写之 ...
- 比较和排序(IComparable和IComparer以及它们的泛型实现)
本文摘要: 1:比较和排序的概念: 2:IComparable和IComparer: 3:IComparable和IComparer的泛型实现IComparable<T>和ICompare ...
- Swift3.0语言教程获取C字符串
Swift3.0语言教程获取C字符串 Swift3.0语言教程获取C字符串,为了让Swift和C语言可以实现很好的交互,开发者可以使用NSString的cString(using:)方法在指定编码格式 ...
- 深圳浩瀚技术有限公司(haohantech)推出的无线移动批发管理PDA解决方案------无线移动POS销售开单系统
办好大型行业展会/交易会使其发挥强大的营销广告宣传作用从而为企业带来巨大的经济效益是每个参展企业的美好愿望. 由于行业内有影响力的展会每年屈指可数, 甚至很多情况下每年就只有一到两次, 如果没能够很好 ...
- 浩瀚科技PDA移动开单|盘点机 数据采集器 条码扫描开单微POS软件 现场打印开单
PDA移动开单,是我公司的一款便携式开单配套产品,PDA能通过蓝牙.无线局域网.互联网直接与主机连接,让公司业务人员能随时随地了解公司产品信息并且进行开单.入库.库存.盘点等一系列进销存操作.是现今企 ...
- js html 一些技巧
0.同步或异步ajax进行提交add update delete等. 在ajax中传参数可以和web端bean对应 1.ajax完成后跳转可以在js中使用 window.location.href=u ...
- PHP 配置文件详解(php.ini 详解 )
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...
- python 代码片段22
#coding=utf-8 class AddressBookEntry(object): version=0.1 def __init__(self, name,phone): self.name ...
- word多级编号,如何让第一级为大写“一”,其他级别均为小写1.
自定义里面设置了第一级为大写,2级.3级首字跟着变为大写,是因为2.3级没有勾选"正规形式编号",如图:
- 翻译:让网络更快一些——最小化浏览器中的回流(reflow)
关于reflowreflow(英音:[ri:’fləu] 美音:[ri’flo])在词典中的解释是回流,逆流.而在web应用中,翻译为回流有些牵强.我个人觉得,理解为回炉(重新塑形),似乎更加形象一点 ...