问题
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();
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/42451456/jschexception-timeout-socket-is-not-established