JSchException timeout: socket not set

I am trying to use JSch to connect to my computer via ssh and then execute a command.

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

Here is my 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 just follow one of my Exec.java examples . I found this answer JSch / SSHJ - Connecting to an SSH server at the click of a button , unfortunately my code looks like it establishes a connection in a similar way, but without results. I tested SSHing on my computer normally and it works fine, so I don’t believe the problem is with the server, but with my code.

Here is the whole 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$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();

+4
1

, 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

Source: https://habr.com/ru/post/1670802/


All Articles