chudyksg
asked on
Java SSH
I want to write a simple program that connects to a server using SSH and jsch library. Then it returns true if it managed to connect and false if it didn't. For authentication I want to use a private key. Anybody who is familiar with jsch can help me with that?
Please post the code you've tried, then we can help.
ASKER
public void ssh() throws URISyntaxException, JSchException {
String fileName = "C:\\Users\\chudy\\Desktop\\Work\\ssh_key\\radek\\radek.ppk";
JSch jsch = new JSch();
jsch.addIdentity(new File(fileName).getAbsolutePath());
}
I am trying the above code to load the private key but I am getting an error:
Exception in thread "main" com.jcraft.jsch.JSchException: invalid privatekey: C:\Users\chudy\Desktop\Work\ssh_key\radek\radek.ppk
at com.jcraft.jsch.IdentityFile.<init>(IdentityFile.java:261)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:135)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:130)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:206)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:192)
at ssh.test.ssh(test.java:26)
at ssh.Main.main(Main.java:22)
Java Result: 1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following works fine for me (proper RSA key)
public static void main(String[] args) throws JSchException {
String fileName = ".ssh/id_rsa";
JSch jsch = new JSch();
jsch.addIdentity(new File(System.getProperty("user.home"), fileName).getAbsolutePath());
}
ASKER
Ok I changed the key to OpenSSH and it is loading now. So my code looks as follows:
and the error I am getting:
public void ssh() throws URISyntaxException, JSchException {
String fileName = "C:\\Users\\chudy\\Desktop\\Work\\ssh_key\\radek\\radek2";
JSch jsch = new JSch();
String user = "radek";
String host = "192.168.1.4";
jsch.addIdentity(new File(fileName).getAbsolutePath());
Session session = jsch.getSession(user, host, 30993);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(15000);
session.connect();
Channel channel = session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
}
and the error I am getting:
Exception in thread "main" com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:461)
at com.jcraft.jsch.Session.connect(Session.java:154)
at ssh.test.ssh(test.java:36)
at ssh.Main.main(Main.java:24)
Java Result: 1
ASKER
Ok I got it working:
public void ssh() throws URISyntaxException, JSchException {
String fileName = "C:\\Users\\chudy\\Desktop\\Work\\ssh_key\\radek\\radek2";
String user = "radek";
String host = "192.168.1.4";
String passphrase = "passphrase";
int port = 30993;
JSch jsch = new JSch();
jsch.addIdentity(new File(fileName).getAbsolutePath(), passphrase);
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(15000);
session.connect();
Channel channel = session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
}
Make sure you can log in from the command line before you try it in Java
>>Ok I got it working:
Good!
Good!
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for chudyksg's comment http:/Q_27381004.html#36917330
for the following reason:
I have solved it myself
Accepted answer: 0 points for chudyksg's comment http:/Q_27381004.html#36917330
for the following reason:
I have solved it myself
I gave you the reason for your problem
:)