java - 如何使用 sshtools SftpClient 和密码?

标签 java sftp j2ssh

我尝试使用 j2ssh SshClient 但没有成功。

我正在尝试使用 RSA 私钥 + 密码打开连接。 我发现了一些我不确定是写入方法的东西:

Properties properties = new Properties();
properties.put("Passphrase", "xyz");
properties.put("PrivateKey", "sftp_rsa");
properties.put("Username", "user");
properties.put("StrictHostKeyChecking", "no");
publicKeyAuthenticationClient.setPersistableProperties(properties);
int result = ssh.authenticate(publicKeyAuthenticationClient);

我正在使用 setPersistableProperties 方法加载保存相关数据的 Properties 对象。 我已将 PrivateKey 设置为文件名,并将 Passphrase 设置为相关的 Passphrase。

我收到的提示是:

The host hostname.host,1.1.1.1 is currently unknown to the system
The host key fingerprint is: 100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Always option disabled, host file is not writeable
Do you want to allow this host key? [Yes|No]

我尝试使用设置为“no”的 StrictHostKeyChecking 属性来删除它。 (当然没有成功)

有什么想法吗?

谢谢!

最佳答案

SshClient.connect 方法采用 HostKeyVerification 对象作为第二个参数,其中一个实现是 IgnoreHostKeyVerification,您可以像这样使用它:

SshConnectionProperties props = new SshConnectionProperties();
props .setHost(server);
props .setPort(port);
sshClient.connect(props , new IgnoreHostKeyVerification());

//this is your code
Properties properties = new Properties();
properties.put("Passphrase", "xyz");
properties.put("PrivateKey", "sftp_rsa");
properties.put("Username", "user");
properties.put("StrictHostKeyChecking", "no");
publicKeyAuthenticationClient.setPersistableProperties(properties);
int result = sshClient.authenticate(publicKeyAuthenticationClient);

我不确定这是否能解决您的问题,但我认为可能就是这样。

关于java - 如何使用 sshtools SftpClient 和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18430947/

相关文章:

java - 如何将 2 个数组项相乘

java - 使用父类的通用生成器创建子类

linux - 从 Windows 到 Linux 的文件传输

java - 在 J2SSH 中断开 NETCONF session 时线程被阻止

Java J2SSH SFTP - 主机 key 无效

java - Java 中数组的 GUI 表示

Java 在 POST 请求中发送带有转义双引号的 JSON 字符串

azure - ##[错误]未处理的: handle is not a Buffer CopyFilesOverSSH in Azure DevOps build pipeline

linux - 从 Linux 到 Windows SFTP 服务器的 Rsync

Java - 使用公钥和密码进行身份验证 - j2ssh