JSchException超时:未建立套接字 [英] JSchException timeout: socket is not established
问题描述
我正在尝试使用 JSch 通过ssh连接到我的计算机,然后运行命令.
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
这是我的相关代码:
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,因此我几乎只是关注他们的一个例子 JSch/SSHJ-连接到SSH服务器按钮,不幸的是,我的代码看起来像是以类似的方式建立了连接,但没有结果.我已经正常测试了SSH进入计算机的过程,并且可以正常工作,因此我不认为问题出在服务器上,而在我的代码上.
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.
这是整个堆栈跟踪:
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的文件.
RectangleClickActivity.java
is the file I am testing SSH.
第97行是:session.connect(10);
第56行是:sshTesting();
推荐答案
问题最终是因为我将配置设置为session.setConfig("StrictHostKeyChecking", "no");
,但是由于某些原因,您需要创建一个配置并设置StrictHostKeyChecking,然后添加它配置为会话,而不是直接设置会话.
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.
工作代码如下:
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);
}
}
这篇关于JSchException超时:未建立套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!