【发布时间】:2017-03-05 12:32:50
【问题描述】:
我得到一个 java.net.ConnectException: failed to connect to 192.168.2.100 (port 22): connect failed: ECONNREFUSED (Connection denied) 当通过 SSH 与 Android 上的JSCH-0.1.54 连接到服务器时。
防火墙配置为允许 IP 地址通过。 从 Windows 到服务器的 SSH 连接可以通过 Putty 进行。 服务器的IP地址没问题。
还能是什么?
我使用的代码如下:
protected Long doInBackground(String... params)
{
try
{
System.setProperty("http.keepAlive", "false");
JSch jsch = new JSch();
Session session = jsch.getSession("root", "192.168.2.100", 22);
session.setPassword("Password");
session.setTimeout(10000);
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
session.setConfig(props);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
if(params[0]=="poweroff")
{
LogPublic("Server wird heruntergefahren");
}
else if(params[0]=="reboot")
{
LogPublic("Server wird neugestartet");
}
channel.setCommand(params[0]);
channel.connect();
channel.disconnect();
session.disconnect();
jsch.removeAllIdentity();
jsch = null;
return new Long(1);
}
catch (Exception ex)
{
LogPublic(ex.getMessage());
return new Long(0);
}
}
【问题讨论】:
-
你能显示异常堆栈吗?另外你的android主机的IP和掩码是什么?您是在连接到网络的真实设备上运行应用程序还是使用模拟器?
-
您能否使用设备上的独立 SSH 终端应用程序连接到主机?
-
@Ahmed Ghonim:我能做到。今天晚些时候将添加它。我现在认为问题出在我的 DHCP... 我的手机与我的 NAS 具有相同的 IP 地址:/ 我正在连接到网络的真实设备上运行该应用程序。
-
@Martin Prikryl:不,我不能,我已经尝试过JuiceSSH
-
好吧,那么你没有编程问题。