Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is possible. Have a look at <a href="http://www.docjar.org/docs/api/com/jcraft/jsch/JSch.html#addIdentity%28String%29" rel="noreferrer"><code>JSch.addIdentity(...)</code></a></p> <p>This allows you to use key either as byte array or to read it from file.</p> <pre><code>import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class UserAuthPubKey { public static void main(String[] arg) { try { JSch jsch = new JSch(); String user = "tjill"; String host = "192.18.0.246"; int port = 10022; String privateKey = ".ssh/id_rsa"; jsch.addIdentity(privateKey); System.out.println("identity added "); Session session = jsch.getSession(user, host, port); System.out.println("session created."); // disabling StrictHostKeyChecking may help to make connection but makes it insecure // see http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no // // java.util.Properties config = new java.util.Properties(); // config.put("StrictHostKeyChecking", "no"); // session.setConfig(config); session.connect(); System.out.println("session connected....."); Channel channel = session.openChannel("sftp"); channel.setInputStream(System.in); channel.setOutputStream(System.out); channel.connect(); System.out.println("shell channel connected...."); ChannelSftp c = (ChannelSftp) channel; String fileName = "test.txt"; c.put(fileName, "./in/"); c.exit(); System.out.println("done"); } catch (Exception e) { System.err.println(e); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload