Programming Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

C Examples

Bounced Mail
Bz2
Certificates/Keys
Charset
CSV
DKIM / DomainKey
Diffie-Hellman
DSA
Email Object
Encryption
FileAccess
FTP
HTML Conversion
HTTP
IMAP
MHT / HTML Email
MIME
NTLM
POP3
RSA
SMTP
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
Tar
Upload
XML
Zip
Amazon S3

 

 

 

 

 

 

 

 

SSH -- Running Commands that Prompt for Additional Input, such as "su"

Demonstrates how to run a shell command via SSH where the shell command prompts for additional input from the client. This example demonstrates "su".

Downloads:

MS Windows Visual C/C++ Libraries
Linux/CentOS C/C++ Libraries
MAC OS X C/C++ Libraries
Solaris C/C++ Libraries
C++ Builder Libraries
FreeBSD C++ Libraries
HP-UX C++ Libraries
BlackBerry QNX C++ Libraries
#include <C_CkSsh.h>

void ChilkatSample(void)
    {
    HCkSsh ssh;
    BOOL success;
    const char * hostname;
    long port;
    long channelNum;
    const char * termType;
    long widthInChars;
    long heightInChars;
    long pixWidth;
    long pixHeight;
    const char * cmd;
    const char * password;

    //  Important: It is helpful to send the contents of the
    //  ssh.LastErrorText property when requesting support.

    ssh = CkSsh_Create();

    //  Any string automatically begins a fully-functional 30-day trial.

    success = CkSsh_UnlockComponent(ssh,"30-day trial");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        return;
    }

    //  Hostname may be an IP address or hostname:
    hostname = "192.168.1.117";
    port = 22;

    //  Keep a session log, which is available via the SessionLog
    //  property:
    CkSsh_putKeepSessionLog(ssh,TRUE);

    success = CkSsh_Connect(ssh,hostname,port);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  When reading, if no additional data arrives for more than
    //  5 seconds, then abort:
    CkSsh_putIdleTimeoutMs(ssh,5000);

    //  SSH Server Authentication
    //  If there is no login/password required, you must still call
    //  AuthenticatePw and use any values for login/password.
    success = CkSsh_AuthenticatePw(ssh,"chilkat","myPassword");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Open a session channel.

    channelNum = CkSsh_OpenSessionChannel(ssh);
    if (channelNum < 0) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Request a pseudo-terminal

    termType = "dumb";

    widthInChars = 120;

    heightInChars = 40;

    pixWidth = 0;

    pixHeight = 0;
    success = CkSsh_SendReqPty(ssh,channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Start a shell on the channel:
    success = CkSsh_SendReqShell(ssh,channelNum);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Send the su command.
    //  (The SSH server I'm using for testing is a Linux Ubuntu
    //  system running OpenSSH.  It is important in this case to send a bare-LF
    //  and not a CRLF.)

    cmd = "su\n";
    success = CkSsh_ChannelSendString(ssh,channelNum,cmd,"ansi");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Read until we get the prompt for the password:
    success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"Password:","ansi",TRUE);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Display what we've received so far:
    printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi"));

    //  Send the password.
    //  Again, make sure it uses a bare-LF and not a CRLF.

    password = "myPassword\n";
    success = CkSsh_ChannelSendString(ssh,channelNum,password,"ansi");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Read the response until we get the shell prompt (assuming it's successful)
    //  In my case, the shell prompt is: "root@ubuntu:/home/chilkat# "
    //  It will be different in your case.
    success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"root@ubuntu:/home/chilkat#","ansi",TRUE);
    if (success != TRUE) {
        //  Check the last-error information and the session log...
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        //  Check to see what was received.
        printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi"));
        return;
    }

    //  Display what we've received so far.  This clears
    //  the internal receive buffer, which is important.
    //  After we send the command, we'll be reading until
    //  the next command prompt.  If the command prompt
    //  is already in the internal receive buffer, we'll think we're
    //  already finished...
    printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi"));

    //  Send a command.  In this case, we are sending the "ls" command:
    cmd = "ls\n";
    success = CkSsh_ChannelSendString(ssh,channelNum,cmd,"ansi");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Read until the next command prompt:
    success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"root@ubuntu:/home/chilkat#","ansi",TRUE);
    if (success != TRUE) {
        //  Check the last-error information and the session log...
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        //  Check to see what was received.
        printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi"));
        return;
    }

    //  Display the command output:
    printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi"));

    //  You may continue sending additional commands.
    //  The technique is: send the command, read until the next command prompt,
    //  and then fetch/clear the internal receive buffer.

    //  We're done, so shut it down..

    //  Send an EOF.  This tells the server that no more data will
    //  be sent on this channel.  The channel remains open, and
    //  the SSH client may still receive output on this channel.
    success = CkSsh_ChannelSendEof(ssh,channelNum);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Close the channel:
    success = CkSsh_ChannelSendClose(ssh,channelNum);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Disconnect
    CkSsh_Disconnect(ssh);

    CkSsh_Dispose(ssh);

    }

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.