web-dev-qa-db-ja.com

JSchExceptionタイムアウト:ソケットが確立されていません

JSch を使用してssh経由でコンピューターに接続し、コマンドを実行しようとしています。

ただし、コードを実行すると、コンピューターに接続することはありません。および次のエラー:

_I/System.out: com.jcraft.jsch.JSchException: timeout: socket is not established_

これが私の関連コードです:

_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を使用するのはこれが初めてなので、それらの例の1つをほぼフォローしています Exec.Java 。私はこの答えを見つけました JSch/SSHJ-ボタンクリックでSSHサーバーに接続しています 残念ながら、私のコードは同様の方法で接続を確立しているように見えますが、結果はありません。私は自分のコンピューターへの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();

5
kalenpw

問題は、構成を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);
    }        

}
2
kalenpw