Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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.

    ssh.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sshPort.i = 22
    success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  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.
    password.s = "mySshPassword"

    success = CkSsh::ckAuthenticatePw(ssh,"mySshLogin",password)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    output.s = CkSsh::ckQuickCommand(ssh,"uname -a","utf-8")
    If CkSsh::ckLastMethodSuccess(ssh) = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    Debug 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.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSsh::ckGetLastJsonData(ssh,json)

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure