java JSchException 超时:未建立套接字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42451456/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:39:30  来源:igfitidea点击:

JSchException timeout: socket is not established

javasocketssshjsch

提问by kalenpw

I am trying to use JSchto connect to my computer through ssh and then run a command.

我正在尝试使用JSch通过 ssh 连接到我的计算机,然后运行命令。

However, when I run the code I never connect to the computer. and the following error:

但是,当我运行代码时,我从未连接到计算机。和以下错误:

I/System.out: com.jcraft.jsch.JSchException: timeout: socket is not established

I/System.out: com.jcraft.jsch.JSchException: timeout: socket is not established

Here is my relevant code:

这是我的相关代码:

protected void sshTesting(){
    String name = "";
    String userName = "kalenpw";
    String password = "hunter2";
    String ip = "192.168.0.4";
    int port = 22;
    String command = "cmus-remote -u";

    try{
        JSch jsch = new JSch();
        Session session = jsch.getSession(userName, ip, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");


        System.out.println("Establishing connection");
        session.connect(10);

        Channel channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        channel.setInputStream(null);
        channel.connect(10000);

//            name = session.getUserName();
//            System.out.println(session.getHost());


    }
    catch(Exception e){
        System.err.print(e);
        System.out.print(e);
    }        

}

This is my first time using JSch so I am pretty much just following one of their examples Exec.java. I have found this answer JSch/SSHJ - Connecting to SSH server on button clickwhich unfortunately my code looks like it is establishing a connection in a similar fashion yet with no results. I have tested SSHing into my computer normally and it works fine so I don't believe the issue is with the server, but with my code.

这是我第一次使用 JSch,所以我几乎只是在关注他们的一个例子Exec.java。我找到了这个答案JSch/SSHJ - Connecting to SSH server on button click不幸的是,我的代码看起来正在以类似的方式建立连接,但没有结果。我已经正常测试了 SSH 连接到我的计算机并且它工作正常,所以我不相信问题出在服务器上,而是出在我的代码上。

Here is the entire stack trace:

这是整个堆栈跟踪:

W/System.err: com.jcraft.jsch.JSchException: timeout: socket is not establishedcom.jcraft.jsch.JSchException: timeout: socket is not established
W/System.err:     at com.jcraft.jsch.Util.createSocket(Util.java:394)
W/System.err:     at com.jcraft.jsch.Session.connect(Session.java:215)
W/System.err:     at com.example.kalenpw.myfirstapp.RectangleClickActivity.sshTesting(RectangleClickActivity.java:97)
W/System.err:     at com.example.kalenpw.myfirstapp.RectangleClickActivity.onCheckedChanged(RectangleClickActivity.java:56)
W/System.err:     at android.widget.CompoundButton.setChecked(CompoundButton.java:165)
W/System.err:     at android.widget.Switch.setChecked(Switch.java:1151)
W/System.err:     at android.widget.Switch.toggle(Switch.java:1146)
W/System.err:     at android.widget.CompoundButton.performClick(CompoundButton.java:123)
W/System.err:     at android.view.View$PerformClick.run(View.java:22589)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7325)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

RectangleClickActivity.javais the file I am testing SSH.

RectangleClickActivity.java是我正在测试 SSH 的文件。

line 97 is: session.connect(10);

第 97 行是: session.connect(10);

line 56 is: sshTesting();

第 56 行是: sshTesting();

采纳答案by kalenpw

The issue ended up being I was setting the config to session.setConfig("StrictHostKeyChecking", "no");, but for some reason you need to create a config and set the StrictHostKeyChecking and then add that config to the session instead of just setting it directly.

问题最终是我将配置设置为session.setConfig("StrictHostKeyChecking", "no");,但出于某种原因,您需要创建一个配置并设置 StrictHostKeyChecking,然后将该配置添加到会话中,而不是直接设置它。

The working code looks like this:

工作代码如下所示:

protected void sshTesting(){
    String name = "";
    String userName = "kalenpw";
    String password = "hunter2";
    String ip = "192.168.0.4";
    int port = 22;
    String command = "cmus-remote -u";

    try{
        JSch jsch = new JSch();
        Session session = jsch.getSession(userName, ip, port);
        session.setPassword(password);

        //Missing code
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        //


        System.out.println("Establishing connection");
        session.connect(10);

        Channel channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        channel.setInputStream(null);
        channel.connect(10000);

//            name = session.getUserName();
//            System.out.println(session.getHost());

    }
    catch(Exception e){
        System.err.print(e);
        System.out.print(e);
    }        

}

回答by migs_peritus

For those that encounter this same exception:

对于那些遇到相同异常的人:

"JSchException timeout: socket is not established"

“JSchException 超时:未建立套接字”

you might need to increase the timeout value in jsch's session properties prior to attempt the connection.

在尝试连接之前,您可能需要在 jsch 的会话属性中增加超时值。

Make sure that a large enough value is set to allow the connection to succeed.

确保设置了足够大的值以允许连接成功。

For example:

例如:

...
session.setTimeout(15000);
session.connect();
...

will set a 15sec timeout on your connection, that should be enough time to establish the socket. In your code above, even though is not well documented, this call is setting 10 millisecs for your connection: ... session.connect(10); ... which is very low. Remove the 10 and leave it as: ... session.connect(); ... Setting the StrictHostKeyChecking property might have solved the problem reducing the time it took the socket to connect but it might not be the right solution as the timeout you have is too small and it will likely fail sporadically.

将在您的连接上设置 15 秒超时,这应该足以建立套接字。在你上面的代码中,即使没有很好的文档记录,这个调用也为你的连接设置了 10 毫秒: ... session.connect(10); ......这是非常低的。删除 10 并将其保留为: ... session.connect(); ...设置 StrictHostKeyChecking 属性可能已经解决了减少套接字连接时间的问题,但它可能不是正确的解决方案,因为您的超时时间太小并且可能会偶尔失败。