Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the JSch API log in to a Unix server without a password?

Tags:

java

ssh

jsch

I'm trying to make a Java application, that executes shell scripts on a remote Unix server, using the JSch API.

I was wondering if it's possible to login to the server without a password. If so - how? Should I generate a pair of authentication keys on the servers, then make the application read information from the key file?

The Java application is on a Windows station.

like image 974
sm1988 Avatar asked Dec 16 '22 14:12

sm1988


1 Answers

Since it took awhile before made it work, here is a whole modified example:

JSch jsch=new JSch();
Session session=jsch.getSession("my_username", "my_host", my_port);
session.setConfig("PreferredAuthentications", "publickey");
jsch.setKnownHosts("~/.ssh/known_hosts");
jsch.addIdentity("~/.ssh/id_rsa");
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000);
Channel channel=session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(3*1000);

Beware whether you have copied rsa or dsa key to the server and add a corresponding identity at line addIdentity - id_rsa or id_dsa.

(cat .ssh/id_rsa.pub | ssh me@servername 'cat >> .ssh/).

like image 116
kimmo Avatar answered Apr 07 '23 17:04

kimmo