Skip to content

k.net.sshClient ​

SSH command execution

Execution ContextPrerequisitesTypical Use
KScript API, background jobsReachable SSH host with valid password or PEM private keyRunning 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 ​

js
let output = ""
k.net.sshClient.passwordConnect(host, user, password, (client) => {
  output = client.runCommand("uname -a", 15)
}, 30)
return { output }

Quick Reference ​

Method / PropertyDescriptionReturns
passwordConnect(host, user, password, callback[, timeout])Connect via SSH with a password and run callbackvoid
privateKeyConnect(host, user, privateKey, callback[, timeout])Connect via SSH with a PEM private key stringvoid
client.runCommand(command[, timeout])Execute a shell command inside the SSH sessionstring

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.

ParameterTypeRequiredDescription
hoststringYesSSH host name or IP
userstringYesLogin user
passwordstringRequired for passwordConnectLogin password
privateKeystringRequired for privateKeyConnectPEM private key content
callback(client) => voidYesCallback after the connection succeeds
timeoutnumberNoConnection timeout in seconds; default is 30

Returns: void. The connection closes after the callback runs.

js
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.

js
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.

ParameterTypeRequiredDescription
commandstringYesShell command to execute
timeoutnumberNoCommand timeout in seconds; default is 30

Returns: string, command output.

js
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 ​

ts
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;
}