Skip to main content

SSH Client

Run commands on the server and transfer files.

Introduction​

Netuno has a SSH client integrated into its low-code framework, which allows connecting to servers, running commands, sending and receiving files, modifying folders, among other remote operations.

It supports multiple simultaneous connections, as well as SCP and SFTP.

This is thanks to the open-source Java implementation SSHJ, which is what allows Netuno to provide these features to all script languages through the _ssh resource.

More about Netuno's SSH resource.

Configuration​

The first step is to configure the SSH access data that Netuno will use to perform the connection.

Navigate within the application you are developing to the environment configuration file. In this example, we will use the development file, located at:

  • config/_development.json

Check if there are settings for SSH; if none exist, then at the end of the configuration file (in JSON format) add the SSH configuration parameters, for example:

{
...
"ssh": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 22,
"username": "root",
"password": "$eCr37"
}
},
...
}

After saving the changes, just restart Netuno to apply the configuration changes. These being the default settings for the connection, as the key name default indicates.

You can add more SSH connection configurations with other keys in the JSON.

To add more SSH connection settings just repeat the default configuration block and change the default key to another of your choice, for example:

{
...
"ssh": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 22,
"username": "root",
"password": "$eCr37"
},
"other": {
"enabled": true,
"host": "my.other-server.net",
"port": 22,
"username": "root",
"password": "$eCr37"
}
},
...
}

The configuration above allows using the default account when coded as follows:

const ssh = _ssh.init()

To use the other configuration simply add the configuration key as a parameter of the _ssh.init() function, as follows:

const sshOther = _ssh.init("other")

If you do not add a configuration name as a parameter, the default account configuration will be used.

Connection​

Before doing anything you need to start the SSH connection:

ssh.connect()

Commands​

To run commands you need to start a command session:

const sshSession = ssh.initSession()

When running a command an object is provided that contains the execution state and the respective data output, for example:

const sshCommandResult = sshSession.exec("ls -rato")
if (sshCommandResult.ok) {
_out.println("Executed successfully:")
_out.println(sshCommandResult.output())
} else {
_out.println("The command failed:")
_out.println(sshCommandResult.error())
}

The object returned by the command is the SSHExecResult.

Remember to always close the session:

sshSession.close()

SCP​

Through SCP we can send and receive files.

To create a text file from a string and download the content of the created file, see the example:

const sshSCP = ssh.initSCP()
// Creates the text file.
sshSCP.uploadText("hello.txt", "Hello! My content here...")
// Downloads and prints the file content.
_out.println(sshSCP.downloadText("hello.txt"))
sshSCP.close()

To perform the upload of files that exist in the application:

const sshSCP = ssh.initSCP()
// Uploads a file that is in storage/filesystem.
sshSCP.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Uploads a file that is in the application root.
sshSCP.upload("document.pdf", _app.file("document.pdf"))
sshSCP.close()

To download files from the server to the application:

const sshSCP = ssh.initSCP()
// Downloads the file to storage/filesystem.
sshSCP.download("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Downloads the file to the application root.
sshSCP.download("document.pdf", _app.file("document.pdf"))
sshSCP.close()

SFTP​

SFTP is a way of transferring and manipulating files and folders that supports the same features as FTP, but using SSH.

To start a new SFTP session:

const sftp = ssh.initSFTP()

Initializes the SSHSFTP object which is the session client.

Create a new folder remotely:

sftp.createDirectory("path/new-folder")

Creates or replaces the remote file with the passed text content:

sftp.uploadText("folder/file.txt", "My content!")

Gets the text content of a remote file:

const remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)

Permanently deletes a remote file:

sftp.deleteFile("folder/file.txt")

Permanently deletes the remote folder:

sftp.deleteDirectory("folder")

Lists all the contents of a folder:

for (const i of sftp.list("path/folder")) {
if (i.directory) {
_out.print("Folder: ")
} else if (i.regularFile) {
_out.print("File: ")
} else {
_out.print("Other: ")
}
_out.println(i.name)
_out.println("<br/>")
}

Returns a list of objects of type SSHFile.

To perform the upload of files that exist in the application:

// Uploads a file that is in storage/filesystem.
sftp.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Uploads a file that is in the application root.
sftp.upload("document.pdf", _app.file("document.pdf"))

To download files from the server to the application:

// Downloads the file to storage/filesystem.
sftp.download("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Downloads the file to the application root.
sftp.download("document.pdf", _app.file("document.pdf"))

Remember to always close the SFTP session:

sftp.close()