Sample code for 30+ languages & platforms
Unicode C

SSH Commands to a Cisco Switch

See more SSH Examples

Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending commands in a device console session. Each command is sent, and its output is read up to the device's next prompt.

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: Network devices differ from general-purpose servers in that they expose a menu- or prompt-driven console rather than a Unix shell, so exec requests are usually unavailable and driving the interactive console is the only option. That makes prompt matching the synchronization mechanism, and it is why a PTY is appropriate here despite the general advice to avoid one. Retrieving the received text between commands is essential: a prompt left in the buffer would make the next receive return immediately with the wrong output.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSshW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSshW ssh;
    int port;
    const wchar_t *password;
    int channelNum;
    BOOL caseSensitive;
    const wchar_t *cmdOutput;

    success = FALSE;

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

    //  Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending
    //  commands in a device console session.
    //  
    //  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.

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

    //  Start a shell session.
    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;

    //  The switch presents a prompt ending in "#".  Reading up to it confirms the session is ready.
    success = CkSshW_ChannelReceiveUntilMatch(ssh,channelNum,L"#",L"utf-8",caseSensitive);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

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

    //  Send a command and read its output up to the next prompt.
    success = CkSshW_ChannelSendString(ssh,channelNum,L"show clock\n",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

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

    //  Retrieving the text also clears the receive buffer, which matters: if the prompt were left
    //  buffered, the next receive would match it immediately and return the wrong output.
    cmdOutput = 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",cmdOutput);

    //  Additional commands follow the same send / read-to-prompt / retrieve pattern.
    success = CkSshW_ChannelSendString(ssh,channelNum,L"show version\n",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshW_lastErrorText(ssh));
        CkSshW_Dispose(ssh);
        return;
    }

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

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

    CkSshW_Disconnect(ssh);


    CkSshW_Dispose(ssh);

    }