【发布时间】:2011-12-05 17:13:19
【问题描述】:
import com.jcraft.jsch.*;
import com.jcraft.jsch.JSchException;
import oracle.jdbc.driver.OracleDriver;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.net.*;
public class SecureFTP {
public static void main(String[] args) throws IOException , ClassNotFoundException, JSchException, SftpException{
JSch jsch = new JSch();
File file = new File("/home/xxxxx/.ssh/id_rsa");
Session session = null;
URL keyFileURL = null;
URI keyFileURI = null;
if (file.exists())
{
keyFileURL = file.toURL();
if (keyFileURL == null)
{
System.out.println("what");
throw new RuntimeException("Key file not found in classpath");
}
}
else System.out.println("FIle not found");
try{
keyFileURI = keyFileURL.toURI();
}
catch(Exception URISyntaxException)
{
System.out.println("Wrong URL");
}
String privateKey = ".ssh/id_rsa";
//jsch.addIdentity(privateKey);
jsch.addIdentity(new File(keyFileURI).getAbsolutePath());
System.out.println(new File(keyFileURI).getAbsolutePath() + " LOL");
session = jsch.getSession("username", "servername");
//session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
// connect
session.connect();
// get SFTP channel
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp schannel = (ChannelSftp) channel;
FileInputStream fis = new FileInputStream(sourcefile);
schannel.put(fis, destinationfile );
fis.close();
}
schannel.exit();
session.disconnect();
}
}
正如您从我注释掉的代码中看到的那样,我已经尽一切可能让它工作,唯一有效的是我直接设置密码。我正在尝试使用生成的 RSA 私钥,但我不断收到身份验证失败。
我已将公钥添加到目标服务器上的授权密钥列表中。而且没有密码。
还有什么我应该做的吗?比如说,在生成密钥时?我缺少一个步骤吗?
我可以使用另一个库来实现相同的功能吗?
【问题讨论】:
-
您能否在服务器上的 syslog 文件中查看为什么身份验证失败?
-
如果这是 Linux 服务器,那么 /var/log/messages 可能吗?询问您的系统管理员或本地 IT。
标签: java solaris rsa sftp private-key