Link to home
Start Free TrialLog in
Avatar of chudyksg
chudyksgFlag for United Kingdom of Great Britain and Northern Ireland

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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post the code you've tried, then we can help.
Avatar of chudyksg

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());
      
    }

Open in new window


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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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());
    }

Open in new window

Ok I changed the key to OpenSSH and it is loading now. So my code looks as follows:


 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();

    }

Open in new window


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

Open in new window

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();

    }

Open in new window

Make sure you can log in from the command line before you try it in Java
>>Ok I got it working:


Good!
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
I gave you the reason for your problem
:)