Sample code for 30+ languages & platforms
Unicode C

Open an SSH Session Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.OpenSessionChannel method, which opens a new SSH session channel. It takes no arguments and returns the channel number, or -1 on failure. The transport must already be connected and authenticated.

Background: SSH multiplexes independent channels over one authenticated connection, and a session channel is the one used to run a command, start a shell, or invoke a subsystem. Opening the channel is only the container — nothing runs until you follow it with an exec, shell, or subsystem request. Always test for a negative return rather than assuming success.

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;

    success = FALSE;

    //  Demonstrates the Ssh.OpenSessionChannel method, which opens a new SSH session channel.  It
    //  takes no arguments and returns the channel number, or -1 on failure.  The transport must
    //  already be connected and authenticated.

    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;
    }

    wprintf(L"Opened session channel: %d\n",channelNum);

    //  Opening the channel does not start a shell or command -- send an exec, shell, or subsystem
    //  request next.

    CkSshW_Disconnect(ssh);


    CkSshW_Dispose(ssh);

    }