我正在尝试使用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,所以我只是关注他们的一个例子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();
答案 0 :(得分:2)
问题最终是我将配置设置为session.setConfig("StrictHostKeyChecking", "no");
,但出于某种原因,您需要创建一个配置并设置StrictHostKeyChecking,然后将该配置添加到会话中,而不是直接设置它。 / p>
工作代码如下所示:
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);
}
}
答案 1 :(得分:0)
对于遇到相同异常的人:
“ JSchException超时:套接字未建立”
在尝试连接之前,您可能需要在jsch的会话属性中增加超时值。
请确保设置了足够大的值以允许连接成功。
例如:
...
session.setTimeout(15000);
session.connect();
...
将为您的连接设置15秒的超时时间,这应该足够建立套接字。 在上面的代码中,即使没有充分记录,此调用也会为您的连接设置10毫秒: ... session.connect(10); ... 这是非常低的。删除10,并将其保留为: ... session.connect(); ... 设置StrictHostKeyChecking属性可能已经解决了减少套接字连接所花费时间的问题,但是由于超时时间太短,可能不是偶然的解决方案,因此它不是正确的解决方案。