我正在尝试编写一个 java 程序,它将通过 ssh 连接并在工作中的服务器(redhat linux)上做一些事情。我的盒子是窗户。我阅读了有关 sshj 的信息,并且正在尝试使示例正常工作。我已经处理了大多数依赖项,现在我在处理公钥/私钥时遇到了错误,不幸的是我也不太了解(是的,这是新手的完美风暴!)。这是错误:
线程“主”net.schmizz.sshj.transport.TransportException 中的异常:[HOST_KEY_NOT_VERIFIABLE] 无法使用端口 22 上的ssh-rsa
指纹验证主机密钥5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3d
myserver
这是代码:
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/** This examples demonstrates how a remote command can be executed. */
public class sshBuddy {
public static void main(String... args)
throws IOException {
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
//ssh.addHostKeyVerifier("5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3d");
ssh.connect("myserver");
try {
ssh.authPublickey(System.getProperty("myusername"));
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
}
}
}
任何帮助将不胜感激,谢谢!