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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const ssh = _ssh.init()
const ssh = _ssh.init()
ssh = _ssh.init()
ssh = _ssh.init()
val ssh = _ssh.init()
final ssh = _ssh.init()
To use the other configuration simply add the configuration key as a parameter of the
_ssh.init() function, as follows:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const sshOther = _ssh.init("other")
const sshOther = _ssh.init("other")
sshOther = _ssh.init("other")
sshOther = _ssh.init("other")
val sshOther = _ssh.init("other")
final sshOther = _ssh.init("other")
If you do not add a configuration name as a parameter, the
defaultaccount configuration will be used.
Connection
Before doing anything you need to start the SSH connection:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
ssh.connect()
ssh.connect()
ssh.connect()
ssh.connect()
ssh.connect()
ssh.connect()
Commands
To run commands you need to start a command session:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const sshSession = ssh.initSession()
const sshSession = ssh.initSession()
sshSession = ssh.initSession()
sshSession = ssh.initSession()
val sshSession = ssh.initSession()
final sshSession = ssh.initSession()
When running a command an object is provided that contains the execution state and the respective data output, for example:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
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())
}
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())
}
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())
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())
end
val 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())
}
final 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
sshSession.close()
sshSession.close()
sshSession.close()
sshSession.close()
sshSession.close()
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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
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()
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()
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()
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()
val 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()
final 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
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()
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()
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()
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()
val 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()
final 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
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()
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()
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()
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()
val 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()
final 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const sftp = ssh.initSFTP()
const sftp = ssh.initSFTP()
sftp = ssh.initSFTP()
sftp = ssh.initSFTP()
val sftp = ssh.initSFTP()
final sftp = ssh.initSFTP()
Initializes the SSHSFTP object which is the session client.
Create a new folder remotely:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.createDirectory("path/new-folder")
sftp.createDirectory("path/new-folder")
sftp.createDirectory("path/new-folder")
sftp.createDirectory("path/new-folder")
sftp.createDirectory("path/new-folder")
sftp.createDirectory("path/new-folder")
Creates or replaces the remote file with the passed text content:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.uploadText("folder/file.txt", "My content!")
sftp.uploadText("folder/file.txt", "My content!")
sftp.uploadText("folder/file.txt", "My content!")
sftp.uploadText("folder/file.txt", "My content!")
sftp.uploadText("folder/file.txt", "My content!")
sftp.uploadText("folder/file.txt", "My content!")
Gets the text content of a remote file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)
const remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)
remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)
remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)
val remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)
final remoteFileContent = sftp.downloadText("folder/file.txt"))
_out.println(remoteFileContent)
Permanently deletes a remote file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.deleteFile("folder/file.txt")
sftp.deleteFile("folder/file.txt")
sftp.deleteFile("folder/file.txt")
sftp.deleteFile("folder/file.txt")
sftp.deleteFile("folder/file.txt")
sftp.deleteFile("folder/file.txt")
Permanently deletes the remote folder:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
Lists all the contents of a folder:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
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/>")
}
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/>")
}
for i in sftp.list("path/folder":
if i.directory:
_out.print("Folder: ")
elif i.regularFile:
_out.print("File: ")
else:
_out.print("Other: ")
_out.println(i.name)
_out.println("<br/>")
}
sftp.list("path/folder").each do |i|
if i.directory
_out.print("Folder: ")
elsif i.regularFile
_out.print("File: ")
else
_out.print("Other: ")
end
_out.println(i.name)
_out.println("<br/>")
end
sftp.list("path/folder").forEach {
if (it.directory) {
_out.print("Folder: ")
} else if (it.regularFile) {
_out.print("File: ")
} else {
_out.print("Other: ")
}
_out.println(it.name)
_out.println("<br/>")
}
for (i in 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// 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"))
// 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"))
# 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"))
# 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"))
// 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"))
// 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
// 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"))
// 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"))
# 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"))
# 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"))
// 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"))
// 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:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.close()
sftp.close()
sftp.close()
sftp.close()
sftp.close()
sftp.close()