Butterfly of Will Beckers

Upload a file to a remote ssh server with java

There is a new feature on our system. We receive from a third party a file. My tests create such a file. Then they upload the file to a remote ssh server with java. The tests are written in java. Afterwards the file is parsed and placed in a database.

The Linux server is only accessible via ssh. This means that the tests can not copy the file to a shared directory. The tests need to transfer the files via sftp.

We use the sshj library to upload the file to the ssh server. The first step is to update our pom.xml file. We use maven to build our test project and to run all tests. Add the sshj library as dependency.

<dependency>
    <groupId>com.hierynomus</groupId>
    <artifactId>sshj</artifactId>
    <version>0.29.0</version>
</dependency>

Upload a file to a server

We create a class that transfers a file to a specific place on our server. The code can look like this:

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

import java.io.File;
import java.io.IOException;

public class FileTransfer {
    public void upload(File localFile) throws IOException {
        SSHClient sshClient = setupSshj();
        SFTPClient sftpClient = sshClient.newSFTPClient();
        sftpClient.put(localFile.getAbsolutePath(), "/file/input/input.txt");
        sftpClient.close();
        sshClient.disconnect();
    }

    private SSHClient setupSshj() throws IOException {
        String username = "username";
        SSHClient client = new SSHClient();
        client.addHostKeyVerifier(new PromiscuousVerifier());
        client.connect("linuxserver");
        client.authPassword(username, "password");
        return client;
    }
}

It is now a simple call to the upload function to transfer the file to the server.

Check if a file is present on a remote server

The system under test removes the file after processing it. That is the only way to see if the process finishes. We create a function to see of the file still exist. If it is not present anymore, we check the results.

import net.schmizz.sshj.sftp.Response;
import net.schmizz.sshj.sftp.SFTPException;

public boolean fileExistOnServer(SFTPClient client) throws IOException {
    try {
        client.stat("/file/input/input.txt");
    } catch (SFTPException e) {
        if (e.getStatusCode() == Response.StatusCode.NO_SUCH_FILE) {
            return false;
        }
        throw e;
    }
    return true;
};

We can check with this function if the upload was succesfull. It is simple to create a function that waits until the file is present now. Create a simple loop around the fileExistOnServer function.

We create a loop around the function to check if the file is still present. If the file is not deleted after a specified time, the function raises an error. This is our waitUntil function.

The sshj library is a very useful library if you want to upload a file to a remote ssh server with java.

About the author

I currently work as a Test Automation Consultant at b.ignited. Here I work for different clients in different industries to help them start and speed up their testing cycles

I’ve been testing software since 2000 when I became involved in testing telephone applications and hardware. Since then, I’ve been able to expand my experience by testing a variety of embedded, web, mobile and desktop applications. I have used various development methodologies from waterfall to agile.

I consider myself to be a lifelong learner.