Sample code for 30+ languages & platforms
C

Get Received SSH Text into a StringBuilder

See more SSH Examples

Demonstrates the Chilkat Ssh.GetReceivedSb method, which decodes a channel's receive buffer and appends the text to a StringBuilder. The arguments are the channel number, the charset, and the StringBuilder. The receive buffer is cleared afterward.

Background: Because this appends rather than replaces, it is the natural choice for accumulating output across repeated reads — call it in a loop and the StringBuilder grows into the complete transcript. It also avoids creating a new string on every retrieval, which matters when a command produces a large amount of text.

Chilkat C Downloads

C
#include <C_CkSsh.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSsh ssh;
    int sshPort;
    const char *password;
    int channelNum;
    HCkStringBuilder sb;
    const char *output;

    success = FALSE;

    //  Demonstrates the Ssh.GetReceivedSb method, which decodes the channel's receive buffer and
    //  appends the text to a StringBuilder.  The 1st argument is the channel number, the 2nd is the
    //  charset, and the 3rd is the StringBuilder.  The receive buffer is cleared afterward.

    ssh = CkSsh_Create();

    sshPort = 22;
    success = CkSsh_Connect(ssh,"ssh.example.com",sshPort);
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        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.
    password = "mySshPassword";

    success = CkSsh_AuthenticatePw(ssh,"mySshLogin",password);
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    channelNum = CkSsh_OpenSessionChannel(ssh);
    if (channelNum < 0) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    success = CkSsh_SendReqExec(ssh,channelNum,"ls -l /tmp");
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    //  IMPORTANT: Set a read timeout.  ReadTimeoutMs defaults to 0, which means no limit -- without
    //  it, this call waits forever if the server never sends channel close.
    CkSsh_putReadTimeoutMs(ssh,15000);

    success = CkSsh_ChannelReceiveToClose(ssh,channelNum);
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    //  Append the received text to a StringBuilder.  Existing content is preserved.
    sb = CkStringBuilder_Create();
    success = CkSsh_GetReceivedSb(ssh,channelNum,"utf-8",sb);
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        CkStringBuilder_Dispose(sb);
        return;
    }

    printf("Characters received: %d\n",CkStringBuilder_getLength(sb));
    output = CkStringBuilder_getAsString(sb);
    printf("%s\n",output);

    CkSsh_Disconnect(ssh);


    CkSsh_Dispose(ssh);
    CkStringBuilder_Dispose(sb);

    }