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?
chudyksgAsked:
Who is Participating?
 
CEHJCommented:
That looks to me like a Putty key, which is probably not what's required. You probably need the key file 'pre-puttification'
0
 
CEHJCommented:
Please post the code you've tried, then we can help.
0
 
chudyksgAuthor Commented:
 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

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
CEHJCommented:
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

0
 
chudyksgAuthor Commented:
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

0
 
chudyksgAuthor Commented:
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

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


Good!
0
 
chudyksgAuthor Commented:
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
0
 
CEHJCommented:
I gave you the reason for your problem
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.