我正在尝试从我的Android应用程序打开SSH客户端会话。尝试连接到本地网络上的设备(Raspberry Pi)。我正在使用SSHJ库版本0.10.0。ssh.connect()
通话失败,TransportException
其中的最终由引起NoSuchAlgorithmException
。请参考下面的异常树。
SSHClient ssh = new SSHClient(new AndroidConfig());
Session session = null;
try {
//ssh.loadKnownHosts();
// Exception thrown on this line
ssh.connect("192.168.1.109", 22);
// Doesn't reach below
ssh.authPassword("user", "password");
session = ssh.startSession();
}
catch (net.schmizz.sshj.transport.TransportException ex) {
;
}
异常树:
net.schmizz.sshj.transport.TransportException
net.schmizz.sshj.common.SSHException
net.schmizz.sshj.common.SSHRuntimeException
java.security.GeneralSecurityException: java.security.NoSuchAlgorithmException: KeyFactory ECDSA implementation not found
java.security.NoSuchAlgorithmException: KeyFactory ECDSA implementation not found
其他系统信息:
SSHJ library : v0.10.0
Android device : Galaxy Note 3 running Android 4.4.2
我使用了Android Studio中的maven依赖关系支持来引入SSHJ JAR,除了SSHJ v0.10.0 jar之外,它还引入了以下三个库...
bouncy castle...
bcpkix-jdk15on-1.50.jar
bcprov-jdk15on-1.50.jar
logging....
slf4j-api-1.7.7.jar
不知道从哪里开始该异常的线索...任何建议表示赞赏!谢谢。
更新:2014年10月31日
根据LeeDavidPainter的建议,我添加了SpongyCastle 1.51.0 JAR,并在顶部添加了以下行:
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
我现在在同一行上遇到另一个异常:
net.schmizz.sshj.transport.TransportException
net.schmizz.sshj.common.SSHException
net.schmizz.sshj.common.SSHRuntimeException
java.security.GeneralSecurityException: java.security.spec.InvalidKeySpecException: key spec not recognised
java.security.spec.InvalidKeySpecException: key spec not recognised
另请注意,我也尝试了以下行,结果相同:
Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
我的手机上还有另一个应用程序,它基本上可以实现我想要实现的功能-名为RaspberryPiController-它通过SSH使用用户名和密码auth连接到RPi。这可以正常工作,因此似乎不是网络问题。
在SSHJ中无法解决此问题,因此决定尝试JSch提供相同功能。它也可以作为Maven仓库使用-我使用的是jsch版本0.1.51('com.jcraft:jsch:0.1.51')。
它第一次使用此代码片段。
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import java.io.ByteArrayOutputStream;
import java.util.Properties;
JSch jsch = new JSch();
com.jcraft.jsch.Session session = null;
String result = "";
try {
session = jsch.getSession("user", "192.168.1.109", 22);
session.setPassword("password");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channel = (ChannelExec)session.openChannel("exec");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
channel.setOutputStream(stream);
// Execute command
channel.setCommand("ls -ltr");
channel.connect(1000);
java.lang.Thread.sleep(500); // this kludge seemed to be required.
channel.disconnect();
result = stream.toString();
}
catch (JSchException ex) {
String s = ex.toString();
System.out.println(s);
}
catch (InterruptedException ex) {
String s = ex.toString();
System.out.println(s);
}
finally {
if (session != null)
session.disconnect();
}
与SSHJ相比,使用它时感觉像是更健壮的实现-或这种印象可能是由于他们选择了非常保守的超时而引起的。例如,如果目标设备已关闭,则默认情况下,session.connect()调用将在尝试放弃连接之前持续尝试约20秒钟。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句