How to list a remote path with JSCH (SFTP) in Android

How to list a remote path with JSCH (SFTP) in Android

Create a sftp client with Java has become really easy using JSCH Library.

JSch is a pure Java implementation of SSH2 (We can use SFTP Channel). 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 is licensed under BSD style license.

You can use the following code to retrieve the content of a remote path (a list) to show the user 

// Remember use the required imports (the library as well) using :

import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;

/// then in our function

try {
     JSch ssh = new JSch();
     Session session = ssh.getSession("username", "myip90000.ordomain.com", 22);
     // Remember that this is just for testing and we need a quick access, you can add an identity and known_hosts file to prevent
     // Man In the Middle attacks
     java.util.Properties config = new java.util.Properties();
     config.put("StrictHostKeyChecking", "no");
     session.setConfig(config);
     session.setPassword("Passw0rd");
     session.connect();

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

     ChannelSftp sftp = (ChannelSftp) channel;
    
     // Now that we have a channel, go to a directory first if we want .. you can give to the ls the path
     // sftp.cd("/var/www/mydirectory");

     @SuppressWarnings("unchecked")
      
     // Get the content of the actual path using ls instruction or use the previous string of the cd instruction
     java.util.Vector<LsEntry> flLst = sftp.ls("/var/www/mydirectory");

     final int i = flLst.size();
			
     // show the info of every folder/file in the console
     for(int j = 0; j<i ;j++){
       LsEntry entry = flLst.get(j);
       SftpATTRS attr = entry.getAttrs();

       System.out.println(entry.getFilename());
       System.out.println(directory + "/" + entry.getFilename()); // Remote filepath
       System.out.println("isDir", attr.isDir()); // Is folder
       System.out.println("isLink", attr.isLink()); // is link
       System.out.println("size",attr.getSize()); // get size in bytes of the file 
       System.out.println("permissions",attr.getPermissions()); // permissions
       System.out.println("permissions_string",attr.getPermissionsString());
       System.out.println("longname",entry.toString());

                            
     }
    
    channel.disconnect();
    session.disconnect();
} catch (JSchException e) {
    System.out.println(e.getMessage().toString());
    e.printStackTrace();  
} catch (SftpException e) {
    System.out.println(e.getMessage().toString());
    e.printStackTrace();
}

The ls function will do the trick for you, it will return a LsEntry with the information of a path and then you can easily get the content of every entry.

Remember that as an example, this doesn't include any security. You need to add the known hosts files if your server uses one, or add an identity if your server uses a Private key for authentication.

This code works on any platform that uses Java and JSCH Library (Android, desktop etc).

Become a more social person