Pages

Using SSH2 in java using jSch

The program SSH (Secure Shell) provides an encrypted channel for logging into another computer over a network, executing commands on a remote computer, and moving files from one computer to another.

SSH provides strong host-to-host and user authentication as well as secure encrypted communications over the Internet.


SSH2 is a more secure, efficient, and portable version of SSH that includes SFTP, which is functionally similar to FTP, but is SSH2 encrypted.

At Indiana University, UITS has upgraded its central systems to SSH2 (usually the OpenSSH version), and encourages those concerned with secure communications to connect using an SSH2 client.

For more information on SSH2 visit.

JSch is a pure Java implementation of SSH2.

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.

For more information on JSch visit

To use SFTP in Java you have to download this jar  and include this "jar"  in your 'classpath' .


Below is a code snippet of SFTP in Java using JSch


package com.xyz.utilities.scp;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SFTP {

    private String hostName;
    private String userName;
    private String password;

    public SFTP(String hostName, String userName, String password) {
        super();
        if (hostName != null && !hostName.isEmpty() && userName != null
                && !userName.isEmpty() && password != null
                && !password.isEmpty()) {
            this.hostName = hostName;
            this.userName = userName;
            this.password = password;
            System.out.println("caleed");
        }

        else {
            System.out.println("caleed");
            throw new NullPointerException("Argument can not be null or empty...");
        }
    }

    public void copy(String source, String destination) {

        try {

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");

            JSch jsch = new JSch();

            Session session = jsch.getSession(this.userName, this.hostName, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();

            System.out.println("Connected");

            String copyFrom = source;
            String copyTo = destination;

            Channel channel = session.openChannel("sftp");

            channel.connect();

            ChannelSftp sftpChannel = (ChannelSftp) channel;
            sftpChannel.put(copyFrom, copyTo);

            sftpChannel.exit();
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    

}

1 comment:

  1. Great article by the great author, it is very massive and informative but still preaches the way to sounds like that it has some beautiful thoughts described so I really appreciate this article. Best How to send long video through email service provider.

    ReplyDelete