Skip to content

k.net.sshClient

SSH command execution

Overview

k.net.sshClient establishes an SSH connection and executes commands inside the callback.

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.

ts
k.net.sshClient.passwordConnect(host, user, password, (client) => {
    const output = client.runCommand("uname -a")
}, 30)
ParameterTypeRequiredDescription
hoststringYesSSH host name or IP
userstringYesLogin user
passwordstringYesLogin password
privateKeystringYesPEM 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.

ts
const privateKey = k.file.read("keys/deploy_rsa.pem")
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.

ts
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", {
        disk,
        kernel
    })
})