java – JSchException timeout:未建立套接字

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

但是,当我运行代码时,我从未连接到计算机.并出现以下错误:

I / System.out:com.jcraft.jsch.JSchException:timeout:未建立套接字

这是我的相关代码:

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);
    }        

}

这是我第一次使用JSch,所以我只是按照他们的一个例子Exec.java.我找到了这个答案JSch/SSHJ – Connecting to SSH server on button click,不幸的是我的代码看起来像是以类似的方式建立连接而没有结果.我已经测试了SSH到我的电脑正常,它工作正常所以我不相信问题是与服务器,但我的代码.

这是整个堆栈跟踪:

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$1.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.java是我正在测试SSH的文件.

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

第56行是:sshTesting();

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

工作代码如下所示:

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);
    }        

}
点赞