Sample code for 30+ languages & platforms
Unicode C

SSH to a Cisco Switch - Processing "More" Responses

See more SSH Examples

Demonstrates connecting to a Cisco switch, entering privileged mode with the ena command, and running a command whose response is paged. Each page ends with --More-- and requires a SPACE character to request the next page.

Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.

Background: Paged output is the console equivalent of a pager like more, and a client must reproduce the keypress a human would make. Matching a set of patterns is what makes this tractable: each read ends either at the device prompt (output complete) or at --More-- (another page waiting), and the code branches accordingly. Note the prompt pattern includes the device name rather than a bare #, because that character also appears inside configuration text and would otherwise match far too early.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSshW.h>
#include <C_CkStringArrayW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSshW ssh;
    int port;
    const wchar_t *password;
    int channelNum;
    BOOL caseSensitive;
    const wchar_t *received;
    const wchar_t *enablePassword;
    HCkStringArrayW saMatch;
    HCkStringBuilderW sbReceived;
    BOOL moreComing;
    const wchar_t *pageText;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates connecting to a Cisco switch, entering privileged mode, and running a command
    //  whose response is paged -- each page ends with "--More--" and requires a SPACE character to
    //  request the next page.

    ssh = CkSshW_Create();

    port = 22;
    success = CkSshW_Connect(ssh,L"172.16.16.100",port);
    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_QuickShell(ssh);
    if (channelNum < 0) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    //  IMPORTANT: Set a read timeout before receiving until a match.  ReadTimeoutMs defaults to 0,
    //  which means no limit -- without it, this call waits forever if the received data never
    //  contains a match.
    CkSshW_putReadTimeoutMs(ssh,15000);

    caseSensitive = TRUE;

    //  In unprivileged mode the switch prompt ends with ">".
    success = CkSshW_ChannelReceiveUntilMatch(ssh,channelNum,L">",L"utf-8",caseSensitive);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    received = 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",received);

    //  Send "ena" to enter privileged mode.  Cisco devices expect a bare CR to terminate a command.
    success = CkSshW_ChannelSendString(ssh,channelNum,L"ena\r",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    success = CkSshW_ChannelReceiveUntilMatch(ssh,channelNum,L"Password:",L"utf-8",caseSensitive);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    received = 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",received);

    //  The enable password is separate from the login password, and likewise should come from a
    //  secure source rather than being hard-coded.
    enablePassword = L"myEnablePassword";
    success = CkSshW_ChannelSendString(ssh,channelNum,enablePassword,L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

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

    //  In privileged mode the prompt ends with "#" instead of ">".
    success = CkSshW_ChannelReceiveUntilMatch(ssh,channelNum,L"#",L"utf-8",caseSensitive);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    received = 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",received);

    //  Run a command whose output is delivered a page at a time.
    success = CkSshW_ChannelSendString(ssh,channelNum,L"show running-config\r",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

    //  Each read ends either at the device prompt (output complete) or at "--More--" (another page
    //  is waiting).  Match the full prompt rather than a bare "#", because "#" also appears inside
    //  configuration text and would match too early.
    saMatch = CkStringArrayW_Create();
    CkStringArrayW_Append(saMatch,L"MySwitchName#");
    CkStringArrayW_Append(saMatch,L"--More--");

    sbReceived = CkStringBuilderW_Create();
    moreComing = TRUE;

    while (moreComing) {
        success = CkSshW_ChannelReceiveUntilMatchN(ssh,channelNum,saMatch,L"utf-8",caseSensitive);
        if (success == FALSE) {
            wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
            CkSshW_Dispose(ssh);
            CkStringArrayW_Dispose(saMatch);
            CkStringBuilderW_Dispose(sbReceived);
            return;
        }

        CkStringBuilderW_Clear(sbReceived);
        pageText = CkSshW_getReceivedText(ssh,channelNum,L"utf-8");
        if (CkSshW_getLastMethodSuccess(ssh) == FALSE) {
            wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
            CkSshW_Dispose(ssh);
            CkStringArrayW_Dispose(saMatch);
            CkStringBuilderW_Dispose(sbReceived);
            return;
        }

        CkStringBuilderW_Append(sbReceived,pageText);
        wprintf(L"%s\n",pageText);

        //  If this page ended with "--More--", send a SPACE to request the next page.
        moreComing = CkStringBuilderW_Contains(sbReceived,L"--More--",caseSensitive);
        if (moreComing) {
            success = CkSshW_ChannelSendString(ssh,channelNum,L" ",L"utf-8");
            if (success == FALSE) {
                wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
                CkSshW_Dispose(ssh);
                CkStringArrayW_Dispose(saMatch);
                CkStringBuilderW_Dispose(sbReceived);
                return;
            }

        }

    }

    CkSshW_Disconnect(ssh);


    CkSshW_Dispose(ssh);
    CkStringArrayW_Dispose(saMatch);
    CkStringBuilderW_Dispose(sbReceived);

    }