Sample code for 30+ languages & platforms
Tcl

Get Diagnostic JSON from the Last SSH Method Call

See more SSH Examples

Demonstrates the Chilkat Ssh.GetLastJsonData method, which copies structured diagnostic information from the most recently called method into a JsonObject. The only argument is the JsonObject that receives the information.

Background: This gives machine-readable diagnostics as an alternative to parsing LastErrorText, which is useful for logging or automated error handling. Many methods produce little or no additional JSON, so expect an object that is sometimes nearly empty. Authentication passwords and private-key passphrases are never included, so the output is safe to log.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Ssh.GetLastJsonData method, which copies structured diagnostic information
#  from the most recently called method into a JsonObject.  The only argument is the JsonObject
#  that receives the information.

set ssh [new_CkSsh]

set sshPort 22
set success [CkSsh_Connect $ssh "ssh.example.com" $sshPort]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  Normally you would not hard-code the password in source.  You should instead obtain it
#  from an interactive prompt, environment variable, or a secrets vault.
set password "mySshPassword"

set success [CkSsh_AuthenticatePw $ssh "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

set output [CkSsh_quickCommand $ssh "uname -a" "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

puts "$output"

#  Retrieve diagnostic details about the call that was just made.  Many methods produce little
#  or no additional JSON.  Passwords and private-key passphrases are never included.
set json [new_CkJsonObject]

CkSsh_GetLastJsonData $ssh $json

CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

CkSsh_Disconnect $ssh

delete_CkSsh $ssh
delete_CkJsonObject $json