Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoSsh.h>
#import <NSString.h>
#import <CkoJsonObject.h>

BOOL success = NO;

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

CkoSsh *ssh = [[CkoSsh alloc] init];

int sshPort = 22;
success = [ssh Connect: @"ssh.example.com" port: [NSNumber numberWithInt: sshPort]];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

//  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.
NSString *password = @"mySshPassword";

success = [ssh AuthenticatePw: @"mySshLogin" password: password];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSString *output = [ssh QuickCommand: @"uname -a" charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSLog(@"%@",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.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[ssh GetLastJsonData: json];

json.EmitCompact = NO;
NSLog(@"%@",[json Emit]);

[ssh Disconnect];