Java JSCH:SFTP。使用端口 21 在 session.connect() 挂起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20559448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSCH: SFTP. Hangs at session.connect() using the port 21
提问by aeycee
I am connecting to FTP server via sftp(JSCH).
我正在通过 sftp(JSCH) 连接到 FTP 服务器。
Evertime i connect to the FTP server using the port 21, it always hangs at session.connect().
每当我使用端口 21 连接到 FTP 服务器时,它总是挂在 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();
}
}
采纳答案by brettw
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.
难道端口 22 是 SFTP 的默认端口?并且在端口 21 上运行的 FTP 服务器不知道如何协商安全 FTP 的对话。基本上,SFTP 是基于 SSH 的 FTP。
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.
编辑:问题是,它正在无限期地等待谈判完成。这是一场双方都没有放弃的墨西哥对峙。使用一些合适的值(3-5 秒)调用session.setTimeout()
beforesession.connect()
或 call session.connect(timeout)
。我相信超时以毫秒为单位。
回答by Michael T
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.
我有一个类似的问题,但端口正确。session.connect() 只是挂起并且不返回任何内容。而其他程序可以成功连接。
The reason was that the host provides more authentication methods, and we must point Jsch to use only password.
原因是主机提供了更多的认证方式,我们必须指出Jsch只使用密码。
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "password");
session.setConfig(config);
回答by Konki
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.
我们正在JSch
从webMethods
服务器运行。升级到新版本后,当我们尝试连接到我们的合作伙伴之一时,它会挂在连接步骤上。
The proposed solution here hasn't been working. I've received this error :
这里提出的解决方案没有奏效。我收到了这个错误:
com.jcraft.jsch.JSchException: Auth fail
com.jcraft.jsch.JSchException:身份验证失败
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.
该Kerberos
用户名和密码不应该在所有的在那里。
After small investigation I've found this page: Skipping Kerberos authentication prompts with JSch
经过小调查,我找到了这个页面: Skipping Kerberos authentication prompts with JSch
After setting PreferredAuthentications
to other values - all has been working fine. The code that I've added:
设置PreferredAuthentications
为其他值后 - 一切正常。我添加的代码:
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();
}
}
回答by Gregor
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
就我而言,仅将 JSch 更新到最新版本(当前为 1.50)即可解决该问题,似乎与旧版本的 JSch 与最新的 OpenSSH 版本 7.1.x 的密钥/身份验证握手不兼容