I am running JDK 1.7 & Windows 7 using netbeans 7.2
I have generated a SSH private & public key pair (SSH2-2048 bits) using putty-keygen. I do not have any password for private key.
I am now trying to connect to one of the host machine using SFTP. But when I pass private key (ppk) to set Identity, code is returning invalid private key error. I used same private key in WinSCP to connect to same host & it is working fine. Kindly help me to resolve the error.
Here is my code:
JSch jsch = new JSch();
Session session = null;
try {
jsch.addIdentity("D:\TEMP\key.ppk");
session = jsch.getSession("tiabscp", "ssiw.support.qvalent.com", 22);
session.setConfig("StrictHostKeyChecking", "no");
//session.setPassword("");
session.connect();
Channel channel = session.openChannel("sftp");
System.out.println("Getting connected");
channel.connect();
System.out.println("connected successfully");
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get("remotefile.txt", "localfile.txt");
sftpChannel.exit();
session.disconnect();
}catch (JSchException e) {
e.printStackTrace();
}catch (SftpException e) {
e.printStackTrace();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…