JSch: like ssh on a server using ssh-keys

I want ssh to the server due to another ssh server. The gateway server requires a username / password, and I can do this. I use a tunnel to go to the next server, but this only requires an ssh key. I created the key via PuTTY, so it exists for my username, but I'm not sure how to get it for my Java program. Is this a configuration? those. setConfig ("userauth.publickey", "com.jcraft.jsch.UserAuthPublicKey"), then how can I use this or something else? The documentation seems sparse and I appreciate any help. All I tried gives me an error: "Auth fail" when connecting this session

Thanks!

The tunnel method that I use: http://sourceforge.net/apps/mediawiki/jsch/index.php?title=ProxySSH , thanks to the guy who wrote it!

In context, I would like to read / write to the server in my school from my Android phone.

+6
source share
1 answer

To enable public key authentication , you need to use one of the JSch.addIdentity methods.

They use the public and private keys in the OpenSSH key format, so make sure you export it from PuTTY in this format. (JSch does not understand the native PuTTY format, although you can write an adapter that implements the Identity interface by disassembling it yourself.)

The personalities added to JSch are global, not sessions. This is usually not a problem, because JSch will check all authentication methods that are supported by itself and the server in order, and public key authentication is usually performed before password authentication.

All authentication methods require a username (usually the name of the login account).

With public key authentication, the public key must be somehow previously available for the server. For OpenSSH sshd, the public key must be specified in ~/.ssh/authorized_keys . (If you have only one public key, just copy it to this file, if you have several (each of which will be allowed), each of them should be on the same line.)

Therefore, after setting the identifier, it should work outside the box.

If you want to make sure that the first session uses password authentication, and the second (tunneled) uses the public key, you can use the configuration per session by overriding the global one:

 tunnelSession.setConfig("PreferredAuthentications", "password"); innerSession.setConfig("PreferredAuthentications", "publickey"); 

(These are comma-separated lists, here is one element.)

About the example of ProxySSH, that is, I (with some help from the author of JSch, Atsuhiko Yamanaka). I should add this information to the Wiki page, maybe.

+9
source

Source: https://habr.com/ru/post/898691/


All Articles