Sample code for 30+ languages & platforms
Unicode C

Get the Type of an Active SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelType method, which returns the type of the channel at a zero-based enumeration index. The only argument is the index — an enumeration position, not a channel number. Values include session, x11, forwarded-tcpip, and direct-tcpip.

Background: Paired with GetChannelNumber, this turns the channel collection into a readable inventory of what a connection is currently doing — which channels are running commands (session) versus tunneling TCP traffic (direct-tcpip). That is valuable for diagnostics and for application code that manages several channel kinds at once. An out-of-range index causes the string-returning method to report failure.

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;
    int n;
    int i;
    int chNum;
    const wchar_t *chType;

    success = FALSE;

    //  Demonstrates the Ssh.GetChannelType method, which returns the type of the channel at a
    //  zero-based enumeration index.  The only argument is the index, which is an enumeration
    //  position and not a channel number.  Values include "session" and "direct-tcpip".

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

    channelNum = CkSshW_OpenSessionChannel(ssh);
    if (channelNum < 0) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    //  Enumerate the active channels and report each one's number and type.
    n = CkSshW_getNumOpenChannels(ssh);

    for (i = 0; i <= n - 1; i++) {
        chNum = CkSshW_GetChannelNumber(ssh,i);
        chType = CkSshW_getChannelType(ssh,i);
        if (CkSshW_getLastMethodSuccess(ssh) == FALSE) {
            wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
            CkSshW_Dispose(ssh);
            return;
        }

        wprintf(L"Channel %d is of type %s\n",chNum,chType);
    }

    CkSshW_Disconnect(ssh);


    CkSshW_Dispose(ssh);

    }