Sample code for 30+ languages & platforms
Unicode C

Release an SSH Channel's Local Resources

See more SSH Examples

Demonstrates the Chilkat Ssh.ChannelRelease method, which releases Chilkat's retained local resources for a channel after all output and exit information has been retrieved. The only argument is the channel number.

Background: Chilkat retains a channel's buffered output and exit information after the channel closes, so you can still read a command's final output. That retention costs memory, and a long-running program that opens many channels should release each one when finished with it. Note this is not a channel-closing method — it sends neither EOF nor CLOSE. Retrieve everything you need first, because releasing discards it.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSshW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSshW ssh;
    int sshPort;
    const wchar_t *password;
    int channelNum;
    const wchar_t *output;

    success = FALSE;

    //  Demonstrates the Ssh.ChannelRelease method, which releases Chilkat's retained local resources
    //  for a channel after all output and exit information has been retrieved.  The only argument is
    //  the channel number.

    ssh = CkSshW_Create();

    sshPort = 22;
    success = CkSshW_Connect(ssh,L"ssh.example.com",sshPort);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_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 = L"mySshPassword";

    success = CkSshW_AuthenticatePw(ssh,L"mySshLogin",password);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    //  Open a session channel.  A negative return value indicates failure.
    channelNum = CkSshW_OpenSessionChannel(ssh);
    if (channelNum < 0) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    success = CkSshW_SendReqExec(ssh,channelNum,L"uname -a");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_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.
    CkSshW_putReadTimeoutMs(ssh,15000);

    //  Retrieve everything of interest before releasing the channel.
    success = CkSshW_ChannelReceiveToClose(ssh,channelNum);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    output = CkSshW_getReceivedText(ssh,channelNum,L"utf-8");
    if (CkSshW_getLastMethodSuccess(ssh) == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    wprintf(L"%s\n",output);

    //  Release the retained local resources.  This does not send EOF or CLOSE.
    CkSshW_ChannelRelease(ssh,channelNum);

    CkSshW_Disconnect(ssh);


    CkSshW_Dispose(ssh);

    }