我正在使用 SSHJ 在远程服务器上执行命令,连接方式如下:
private static void connect() {
try {
client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
client.connect(HOST);
String path = System.getProperty("/**/user.home") + "/.ssh/id_rsa";
client.authPublickey(USER, client.loadKeys(path));
} catch (IOException e) {
logErrorAndRethrowException("Error connecting to server via SSH", e);
}
}
当我在我的 IDE 中运行它时,它运行没有问题。但是,当我尝试使用 Gradle 在 Jenkins 环境中运行它时,我得到以下堆栈跟踪:
Exception in thread "reader" java.lang.NoClassDefFoundError: org/bouncycastle/openssl/PEMParser
at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.readKeyPair(PKCS8KeyFile.java:131)
at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.getPrivate(PKCS8KeyFile.java:72)
at net.schmizz.sshj.userauth.method.KeyedAuthMethod.putSig(KeyedAuthMethod.java:59)
at net.schmizz.sshj.userauth.method.AuthPublickey.sendSignedReq(AuthPublickey.java:74)
at net.schmizz.sshj.userauth.method.AuthPublickey.handle(AuthPublickey.java:45)
at net.schmizz.sshj.userauth.UserAuthImpl.handle(UserAuthImpl.java:143)
at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:511)
at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:107)
at net.schmizz.sshj.transport.Decoder.received(Decoder.java:175)
at net.schmizz.sshj.transport.Reader.run(Reader.java:60)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.openssl.PEMParser
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
我尝试将 bouncycastle 添加为单独的依赖项(bcprov 和 bcpkix 包),即使 SSHJ 引入了它,我已经使用了一堆版本,我尝试过启用和禁用 Jenkins bouncycastle 插件。我也尝试过使用 schmizz SSHJ 而不是 Hierynomus。
我正在使用 Java 8、Gradle 3.2.1(原因)、Jenkins 2.190.3 和 Hierynomus SSHJ 0.27。