I am using JSch for sftp communication, now i want to use facilitate the key-based authentication, key is loaded on client and server machine once by my network team and all later communication would be only user based for which we have loaded the key.
sftp -oPort=10022 [email protected]
like this command work fine and connect to the sftp, how i can achieve this functionality programmatically.
if it is not possible using JSch, please suggest some other library. I came across Apache SSHD.
JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
JSch. JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.
JSch is a Java implementation for the SSH2 protocol. It allows you to connect to an OpenSSH server through the sshd process and use secure file transferring. In addition, it also allows you to use port forwarding and X11 forwarding. JSch supports SSH File Transfer Protocol(version 0, 1, 2, 3).
No, it's not risky to give JSch your private key. In order to make asymmetric cryptography work, you have to use a private key. In this case, JSch is doing the job for you, but it won't send it to anyone, it's just using it to decrypt data you receive, and encrypt data you send.
It is possible. Have a look at JSch.addIdentity(...)
This allows you to use key either as byte array or to read it from file.
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); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With