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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Please post the code you've tried, then we can help.
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

CEHJCommented:
That looks to me like a Putty key, which is probably not what's required. You probably need the key file 'pre-puttification'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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

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

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

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


Good!
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
CEHJCommented:
I gave you the reason for your problem
CEHJCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.