【发布时间】:2014-10-16 02:12:24
【问题描述】:
我需要 SCP 文件(例如 csv 文件)到 java 程序中的另一台服务器。 SCP 的 RSA 密钥存储在 java 密钥库中。
我找不到任何允许这样做的代码。
谁能提供一些示例代码或有关如何执行此操作的想法?
(我发现了一些与 id_rsa 字符串一起使用的代码。但它们是不同的格式。事实证明,尝试提取/转换为该格式很困难)
【问题讨论】:
我需要 SCP 文件(例如 csv 文件)到 java 程序中的另一台服务器。 SCP 的 RSA 密钥存储在 java 密钥库中。
我找不到任何允许这样做的代码。
谁能提供一些示例代码或有关如何执行此操作的想法?
(我发现了一些与 id_rsa 字符串一起使用的代码。但它们是不同的格式。事实证明,尝试提取/转换为该格式很困难)
【问题讨论】:
Here 和here 是有关将密钥从 java 密钥库转换为 .pem 文件的一些信息。然后就可以用pem来scp了。
试试sshj 库。以下是 SCP 上传示例:
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.xfer.FileSystemFile;
import java.io.File;
import java.io.IOException;
/** This example demonstrates uploading of a file over SCP to the SSH server. */
public class SCPUpload {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey("/path/to/key.pem"));
// Present here to demo algorithm renegotiation - could have just put this before connect()
// Make sure JZlib is in classpath for this to work
ssh.useCompression();
final String src = System.getProperty("user.home") + File.separator + "test_file";
ssh.newSCPFileTransfer().upload(new FileSystemFile(src), "/tmp/");
} finally {
ssh.disconnect();
}
}
}
【讨论】: