SSH Client
Run commands on the server and transfer files.
Introduction
Netuno has an SSH client integrated into its low-code framework, which allows you to connect to servers, execute commands, send and receive files, modify folders, and perform other remote operations.
It supports multiple simultaneous connections, as well as SCP and SFTP.
This, thanks to the Java and open-source implementation
SSHJ, is what allows Netuno
to provide these features to all scripting languages through the _ssh
feature.
More about Netuno's SSH feature.
Configuration
The first step will be to configure the SSH access data that Netuno will use to connect.
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 for SSH configuration settings. If not, add the SSH configuration parameters at the end of the configuration file (in JSON format), for example:
{
...
"ssh": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 22,
"username": "root",
"password": "$eCr37"
}
},
...
}
After saving your changes, simply restart Netuno to apply the configuration changes. These are the default
settings for the connection, as the default
key name indicates.
You can add additional SSH connection configurations using other keys in the JSON.
To add additional SSH connection configurations, simply repeat the default configuration block and replace
the default
key with one of your choice, for example:
{
...
"ssh": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 22,
"username": "root",
"password": "$eCr37"
},
"other": {
"enabled": true,
"host": "other.server.net",
"port": 22,
"username": "root",
"password": "$eCr37"
}
},
...
}
The above configuration allows you to use the default
account when coded as follows:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const ssh = _ssh.init()
ssh = _ssh.init()
ssh = _ssh.init()
val ssh = _ssh.init()
def ssh = _ssh.init()
To use the other
configuration, simply add the configuration key as a parameter to the _ssh.init()
function, as follows:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sshOther = _ssh.init("other")
sshOther = _ssh.init("other")
sshOther = _ssh.init("other")
val sshOther = _ssh.init("other")
def 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:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ssh.connect()
ssh.connect()
ssh.connect()
ssh.connect()
ssh.connect()
Commands
To run commands, you need to start a command session:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sshSession = ssh.initSession()
sshSession = ssh.initSession()
sshSession = ssh.initSession()
val sshSession = ssh.initSession()
def sshSession = ssh.initSession()
When executing a command, an object is provided that contains the execution status and the respective output, for example:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sshCommandResult = sshSession.exec("ls -rato")
if (sshCommandResult.ok) {
_out.println("Successfully executed:")
_out.println(sshCommandResult.output)
} else {
_out.println("The command failed:")
_out.println(sshCommandResult.error)
}
sshCommandResult = sshSession.exec("ls -rato")
if sshCommandResult.ok:
_out.println("Successfully executed:")
_out.println(sshCommandResult.output)
else:
_out.println("The command failed:")
_out.println(sshCommandResult.error)
sshCommandResult = sshSession.exec("ls -rato")
if sshCommandResult.ok
_out.println("Successfully executed:")
_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("Successfully executed:")
_out.println(sshCommandResult.output)
} else {
_out.println("The command failed:")
_out.println(sshCommandResult.error)
}
def sshCommandResult = sshSession.exec("ls -rato")
if (sshCommandResult.ok) {
_out.println("Successfully executed:")
_out.println(sshCommandResult.output)
} else {
_out.println("The command failed:")
_out.println(sshCommandResult.error)
}
The object returned by the command is SSHExecResult.
Remember to always close the session:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
sshSession.close()
sshSession.close()
sshSession.close()
sshSession.close()
sshSession.close()
SCP
Using SCP, we can send and receive files.
To create a text file from a string and download the contents of the created file, see the example:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sshSCP = ssh.initSCP()
// Create the text file.
sshSCP.uploadText("hello.txt", "Hello! My content here...")
// Download and print the contents of the file.
_out.println(sshSCP.downloadText("hello.txt"))
sshSCP.close()
sshSCP = ssh.initSCP()
# Create the text file.
sshSCP.uploadText("hello.txt", "Hello! My content here...")
# Download and print the contents of the file.
_out.println(sshSCP.downloadText("hello.txt"))
sshSCP.close()
sshSCP = ssh.initSCP()
# Create the text file.
sshSCP.uploadText("hello.txt", "Hello! My content here...")
# Download and print the contents of the file.
_out.println(sshSCP.downloadText("hello.txt"))
sshSCP.close()
val sshSCP = ssh.initSCP()
// Create the text file.
sshSCP.uploadText("hello.txt", "Hello! My content here...")
// Download and print the contents of the file.
_out.println(sshSCP.downloadText("hello.txt"))
sshSCP.close()
def sshSCP = ssh.initSCP()
// Create the text file.
sshSCP.uploadText("hello.txt", "Hello! My content here...")
// Download and print the contents of the file.
_out.println(sshSCP.downloadText("hello.txt"))
sshSCP.close()
To upload existing files in the application:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sshSCP = ssh.initSCP()
// Sends a file that is in storage/filesystem.
sshSCP.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Upload a file that is in the application root.
sshSCP.upload("document.pdf", _app.file("document.pdf"))
sshSCP.close()
sshSCP = ssh.initSCP()
# Sends a file that is in storage/filesystem.
sshSCP.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
# Upload a file that is in the application root.
sshSCP.upload("document.pdf", _app.file("document.pdf"))
sshSCP.close()
sshSCP = ssh.initSCP()
# Sends a file that is in storage/filesystem.
sshSCP.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
# Upload a file that is in the application root.
sshSCP.upload("document.pdf", _app.file("document.pdf"))
sshSCP.close()
val sshSCP = ssh.initSCP()
// Sends a file that is in storage/filesystem.
sshSCP.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Upload a file that is in the application root.
sshSCP.upload("document.pdf", _app.file("document.pdf"))
sshSCP.close()
def sshSCP = ssh.initSCP()
// Sends a file that is in storage/filesystem.
sshSCP.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Upload a file that is in the application root.
sshSCP.upload("document.pdf", _app.file("document.pdf"))
sshSCP.close()
Para baixar (download) arquivos do servidor para a aplicação:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sshSCP = ssh.initSCP()
// Downloads the file to storage/filesystem.
sshSCP.download("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Download 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())
# Download 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())
# Download 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())
// Download the file to the application root.
sshSCP.download("document.pdf", _app.file("document.pdf"))
sshSCP.close()
def sshSCP = ssh.initSCP()
// Downloads the file to storage/filesystem.
sshSCP.download("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Download the file to the application root.
sshSCP.download("document.pdf", _app.file("document.pdf"))
sshSCP.close()
SFTP
SFTP is a way to transfer and manipulate files and folders that supports the same functionality as FTP, but using SSH.
To start a new SFTP session:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const sftp = ssh.initSFTP()
sftp = ssh.initSFTP()
sftp = ssh.initSFTP()
val sftp = ssh.initSFTP()
def sftp = ssh.initSFTP()
Starts the [SSH SFTP](/docs/library/objects/SSH SFTP) object, which is the session client.
Create a new folder remotely:
- JavaScript
- 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")
Creates or replaces the remote file with the passed text content:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.uploadText("path/file.txt", "My content!")
sftp.uploadText("path/file.txt", "My content!")
sftp.uploadText("path/file.txt", "My content!")
sftp.uploadText("path/file.txt", "My content!")
sftp.uploadText("path/file.txt", "My content!")
Gets the text contents of a remote file:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const contentRemoteFile = sftp.downloadText("path/file.txt"))
_out.println(contentRemoteFile)
contentRemoteFile = sftp.downloadText("path/file.txt"))
_out.println(contentRemoteFile)
contentRemoteFile = sftp.downloadText("path/file.txt"))
_out.println(contentRemoteFile)
val contentRemoteFile = sftp.downloadText("path/file.txt"))
_out.println(contentRemoteFile)
def contentRemoteFile = sftp.downloadText("path/file.txt"))
_out.println(contentRemoteFile)
Permanently delete a remote file:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.deleteFile("path/file.txt")
sftp.deleteFile("path/file.txt")
sftp.deleteFile("path/file.txt")
sftp.deleteFile("path/file.txt")
sftp.deleteFile("path/file.txt")
Permanently delete the remote folder:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
sftp.deleteDirectory("folder")
List all the contents of a folder:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
for (const i of sftp.list("path/folder")) {
if (i.directory) {
_out.print("Directory: ")
} 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("Directory: ")
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("Directory: ")
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("Directory: ")
} 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("Directory: ")
} 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 upload existing files in the application:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
// Sends a file that is in storage/filesystem.
sftp.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Upload a file that is in the application root.
sftp.upload("document.pdf", _app.file("document.pdf"))
# Sends a file that is in storage/filesystem.
sftp.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
# Upload a file that is in the application root.
sftp.upload("document.pdf", _app.file("document.pdf"))
# Sends a file that is in storage/filesystem.
sftp.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
# Upload a file that is in the application root.
sftp.upload("document.pdf", _app.file("document.pdf"))
// Sends a file that is in storage/filesystem.
sftp.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Upload a file that is in the application root.
sftp.upload("document.pdf", _app.file("document.pdf"))
// Sends a file that is in storage/filesystem.
sftp.upload("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Upload 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
- Python
- Ruby
- Kotlin
- Groovy
// Downloads the file to storage/filesystem.
sftp.download("spreadsheet.xlsx", _storage.filesystem("server", "spreadsheet.xlsx").file())
// Download 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())
# Download 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())
# Download 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())
// Download 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())
// Download the file to the application root.
sftp.download("document.pdf", _app.file("document.pdf"))
Remember to always close the SFTP session:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
sftp.close()
sftp.close()
sftp.close()
sftp.close()
sftp.close()