I am trying to use JSch to connect to my computer through ssh and then run 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 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 click which 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.
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$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
is the file I am testing SSH.
line 97 is: session.connect(10);
line 56 is: sshTesting();
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.
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);
}
}
For those that encounter this same exception:
"JSchException timeout: socket is not established"
you might need to increase the timeout value in jsch's session properties prior to attempt the connection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With