[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java SSH

Posted on 2011-10-05
11
Medium Priority
?
1,216 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:chudyksg
  • 7
  • 4
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36916314
Please post the code you've tried, then we can help.
0
 

Author Comment

by:chudyksg
ID: 36916406
 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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36916473
That looks to me like a Putty key, which is probably not what's required. You probably need the key file 'pre-puttification'
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 36916589
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
 

Author Comment

by:chudyksg
ID: 36917220
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
 

Author Comment

by:chudyksg
ID: 36917330
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36917347
Make sure you can log in from the command line before you try it in Java
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36917445
>>Ok I got it working:


Good!
0
 

Author Comment

by:chudyksg
ID: 36918398
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36918399
I gave you the reason for your problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36920579
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question