k.net.sshClient
SSH command execution
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, background jobs | Reachable SSH host with valid password or PEM private key | Running remote operational scripts, health checks, or deploy commands |
k.net.sshClient establishes an SSH connection and runs commands via ConnectedClient.runCommand() inside the callback.
Quick Start
let output = ""
k.net.sshClient.passwordConnect(host, user, password, (client) => {
output = client.runCommand("uname -a", 15)
}, 30)
return { output }Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
passwordConnect(host, user, password, callback[, timeout]) | Connect via SSH with a password and run callback | void |
privateKeyConnect(host, user, privateKey, callback[, timeout]) | Connect via SSH with a PEM private key string | void |
client.runCommand(command[, timeout]) | Execute a shell command inside the SSH session | string |
passwordConnect() / privateKeyConnect()
passwordConnect(host, user, password, callback, timeout?) connects with a password; privateKeyConnect(host, user, privateKey, callback, timeout?) connects with a PEM private key string.
| Parameter | Type | Required | Description |
|---|---|---|---|
host | string | Yes | SSH host name or IP |
user | string | Yes | Login user |
password | string | Required for passwordConnect | Login password |
privateKey | string | Required for privateKeyConnect | PEM private key content |
callback | (client) => void | Yes | Callback after the connection succeeds |
timeout | number | No | Connection timeout in seconds; default is 30 |
Returns: void. The connection closes after the callback runs.
k.net.sshClient.passwordConnect(host, user, password, (client) => {
const output = client.runCommand("uname -a")
k.logger.information("SSH", output)
}, 30)When using key authentication, load the PEM private key string from a non-public server-side store and pass it in:
Never store private keys in k.file
By default, Kooboo sites have EnableFileIOUrl = true, meaning files stored via k.file are publicly downloadable by anonymous HTTP clients at /__kb/kfile/<path> (unless EnableFileIOUrl is disabled or denyAnonymousFileIOAccessRule is configured). Store PEM private keys in a non-public store such as k.DB.keyValue (up to 4096 UTF-8 bytes per entry), k.DB.sqlite / k.DB.indexedDb, or server-only configuration, and never expose them via public endpoints.
const keyEntry = k.DB.keyValue.get("ssh_deploy_key")
const privateKey = keyEntry ? keyEntry.toString() : ""
if (!privateKey) {
throw new Error("Missing SSH private key")
}
k.net.sshClient.privateKeyConnect(host, user, privateKey, (client) => {
const output = client.runCommand("systemctl status nginx", 10)
k.logger.information("SSH", output)
})ConnectedClient.runCommand()
Execute a shell command and return its output.
| Parameter | Type | Required | Description |
|---|---|---|---|
command | string | Yes | Shell command to execute |
timeout | number | No | Command timeout in seconds; default is 30 |
Returns: string, command output.
k.net.sshClient.passwordConnect(host, user, password, (client) => {
const disk = client.runCommand("df -h /", 10)
const kernel = client.runCommand("uname -a")
k.logger.information("SSH health", JSON.stringify({
disk,
kernel
}))
})TypeScript Definitions
interface SshClient {
passwordConnect(host: string, user: string, password: string, callback: (p1: ConnectedClient) => void, timeout?: number): void;
privateKeyConnect(host: string, user: string, privateKey: string, callback: (p1: ConnectedClient) => void, timeout?: number): void;
}
interface ConnectedClient {
runCommand(command: string, timeout?: number): string;
}