Show Every File on a Remote Server in Java
When working with remote servers, it is common to need to interact with the file system, such as listing files in a directory. In Java, several libraries allow us to establish an SSH connection and interact with remote servers. This tutorial will guide you through the process using three popular libraries: JSch, Apache Mina SSHD, and SSHJ.
1. Prerequisites
- Java Development Kit (JDK) installed
- Maven (or another build tool)
- Access to a remote server with SSH enabled
- A private key for authentication
2. Apache Mina SSHD Library
Apache Mina SSHD is a feature-rich SSH library that provides a full-fledged SSH client and server implementation. It is ideal for applications that need advanced SSH features or require high-performance SSH communication.
2.1 Setting Up the Project
We need to include the relevant dependencies in our file to use Apache Mina SSHD for SSH and SFTP operations. Specifically, you need the sshd-core
library for basic SSH functionality and sshd-sftp
for SFTP operations.
<!-- Apache Mina SSHD Core for SSH functionalities --> <dependency> <groupId>org.apache.sshd</groupId> <artifactId>sshd-core</artifactId> <version>2.13.2</version> </dependency> <!-- Apache Mina SSHD SFTP for SFTP functionalities --> <dependency> <groupId>org.apache.sshd</groupId> <artifactId>sshd-sftp</artifactId> <version>2.13.2</version> </dependency>
2.2 Connecting to the Remote Server
In this example, we will connect to a server using a private key and then use the ScpClient
to interact with the file system.
import org.apache.sshd.client.SshClient; import org.apache.sshd.client.session.ClientSession; import java.io.IOException; import java.nio.file.Paths; import org.apache.sshd.common.keyprovider.FileKeyPairProvider; import java.security.KeyPair; import org.apache.sshd.sftp.client.SftpClient; import org.apache.sshd.sftp.client.SftpClientFactory; import org.apache.sshd.sftp.client.SftpClient.DirEntry; import java.util.List; import java.util.stream.*; public class MinaSSHDExample { private static final String USER = "your-username"; private static final String HOST = "your-remote-host"; private static final int PORT = 22; private static final String PRIVATE_KEY_PATH = "path/to/your/private/key"; public static void main(String[] args) throws IOException { try (SshClient client = SshClient.setUpDefaultClient()) { client.start(); try (ClientSession session = client.connect(USER, HOST, PORT).verify().getSession()) { FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(Paths.get(PRIVATE_KEY_PATH)); Iterable<KeyPair> keyPairs = fileKeyPairProvider.loadKeys(null); for (KeyPair keyPair : keyPairs) { session.addPublicKeyIdentity(keyPair); } session.auth().verify(); SftpClientFactory factory = SftpClientFactory.instance(); SftpClient sftpClient = factory.createSftpClient(session); listFiles(sftpClient, "/var/log"); } client.stop(); } catch (IOException e) { } } private static void listFiles(SftpClient sftpClient, String path) throws IOException { // List all entries in the remote directory Iterable<DirEntry> iterator = sftpClient.readDir(path); List<DirEntry> entries = StreamSupport.stream(iterator.spliterator(), false) .collect(Collectors.toList()); for (DirEntry entry : entries) { System.out.println(entry.getFilename()); } } }
2.3 Listing Files in a Directory
The listFiles
method in this code utilizes the SftpClient
to interact with the remote server to list all files in a specified remote directory. It first retrieves an Iterable
of DirEntry
objects from the readDir
method of the SftpClient
, which represents directory entries. These entries are then converted into a List
using Java Streams for easier processing.
Output could be:
syslog auth.log kern.log
3. JSch Library
JSch is a widely-used Java library that allows you to connect to a remote server via SSH. It is lightweight and easy to use, making it a popular choice for simple SSH tasks like executing commands, uploading, downloading files, and more.
3.1 Setting Up the Project
Add the JSch dependency to your pom.xml
:
<dependency> <groupId>com.github.mwiede</groupId> <artifactId>jsch</artifactId> <version>0.2.18</version> </dependency>
3.2 Connecting to the Remote Server
JSch makes it straightforward to establish an SSH connection using a private key for authentication. This example demonstrates how to set up a connection to a remote server and interact with its file system. Once connected, you can use SFTP to access and list the contents of directories on the server.
import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import java.util.Properties; import java.util.Vector; public class JschExample { private static final String USER = "your-username"; private static final String HOST = "your-remote-host"; private static final int PORT = 22; private static final String PRIVATE_KEY_PATH = "path/to/your/private/key"; public static void main(String[] args) { JSch jsch = new JSch(); Session session = null; try { jsch.addIdentity(PRIVATE_KEY_PATH); session = jsch.getSession(USER, HOST, PORT); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); listFiles(sftpChannel, "/home/user/documents"); sftpChannel.disconnect(); session.disconnect(); } catch (Exception e) { } } private static void listFiles(ChannelSftp sftpChannel, String path) throws Exception { Vector<LsEntry> fileList = sftpChannel.ls(path); for (int i = 0; i < fileList.size(); i++) { LsEntry entry = (LsEntry) fileList.get(i); System.out.println(entry.getFilename()); } } }
This code begins by creating an instance of JSch
, a class that handles SSH connections. The addIdentity
method is used to load the private key for authentication. A Session
object is then created with the remote server’s user, host, and port information.
To avoid strict host key checking (which is necessary for establishing a connection without verifying the server’s fingerprint), a configuration property is set. The session is then connected, and an SFTP channel is opened using the openChannel
method. The SFTP channel allows for file transfer and directory operations.
3.3 Listing Files in a Directory
The listFiles
method uses the SFTP channel to run the ls
command, which lists all files in the remote directory. The ls
command retrieves a list of entries (files and directories) within the specified directory. Each entry is processed using a for-each
loop, where each item is cast to a ChannelSftp.LsEntry
. The getFilename
method is then called on each entry to print the file or directory name to the console.
You might see output like:
file1.txt report.pdf presentation.pptx
4. SSHJ Library
SSHJ is a modern and intuitive SSH library for Java that provides a high-level API for SSH operations.
4.1 Setting Up the Project
Add the following SSHJ dependency to your pom.xml
:
<dependency> <groupId>com.hierynomus</groupId> <artifactId>sshj</artifactId> <version>0.38.0</version> </dependency>
4.2 Connecting to the Remote Server
With SSHJ, establishing an SSH connection and handling file operations is straightforward.
import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.sftp.SFTPClient; import net.schmizz.sshj.sftp.RemoteResourceInfo; import java.io.IOException; import java.util.List; import net.schmizz.sshj.transport.verification.PromiscuousVerifier; public class SSHJExample { private static final String USER = "your-username"; private static final String HOST = "your-remote-host"; private static final String PRIVATE_KEY_PATH = "path/to/your/private/key"; public static void main(String[] args) throws IOException { SSHClient sshClient = new SSHClient(); try { // Disabling host key verification (not recommended for production) sshClient.addHostKeyVerifier(new PromiscuousVerifier()); // Connect to the remote server sshClient.connect(HOST); // Authenticate with a private key sshClient.authPublickey(USER, PRIVATE_KEY_PATH); try ( // Create an SFTP client SFTPClient sftpClient = sshClient.newSFTPClient()) { // List files in the remote directory listFiles(sftpClient, "/home/remoteuser/projects"); } sshClient.disconnect(); } catch (IOException e) { } } private static void listFiles(SFTPClient sftpClient, String path) throws IOException { List<RemoteResourceInfo> files = sftpClient.ls(path); for (RemoteResourceInfo file : files) { System.out.println("Filename: " + file.getName()); System.out.println("Permissions: " + file.getAttributes().getPermissions()); } } }
In this code, an SSHClient
instance is created, which manages the SSH connection to the remote server. The addHostKeyVerifier
method is called with a custom implementation to bypass host key verification (though in production, you would typically verify the server’s host key to prevent man-in-the-middle attacks). The client then connects to the specified remote host and authenticates using the provided username and private key.
An SFTPClient
is created to handle file operations, and within a try-with-resources block, the listFiles
method is called to list and print details (name and permissions) of all files in the specified remote directory.
4.3 Listing Files in a Directory
The listFiles
method takes advantage of the SFTPClient
to list files within a specified directory on a remote server. The ls
method returns a list of RemoteResourceInfo
objects, each representing a file or directory in the remote path. This information includes file attributes such as the name and permissions.
The output might be:
Filename: data1.csv Permissions: rw-r--r-- Filename: data2.json Permissions: rw-r--r-- Filename: backup Permissions: rwxr-xr-x Filename: latest-link Permissions: lrwxrwxrwx
5. Conclusion
In this article, we explored how to interact with remote servers using JSch, Apache Mina SSHD, and SSHJ libraries. We demonstrated how to set up the Maven project with the necessary dependencies, establish an SSH connection, and perform file operations such as listing directory contents.
6. Download the Source Code
This article covered how to show and list every file on a remote server using Java.
You can download the full source code of this example here: show every file from a remote server in Java