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 Firewall - Login and Run Shell Commands on Firewall

Demonstrates how to start a shell on a SonicWALL firewall and execute commands.

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

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

    //  Connect to a firewall with SSH admin capabilities:

    //  Hostname may be an IP address or hostname:
    hostname = "192.168.1.106";
    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,"myLogin","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;
    }

    //  The SonicWALL firewall will send prompts for
    //  username and password.  Read to get the first prompt:
    success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"User:","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 our firewall username.
    //  Note, we must end the command with a bare-LF because
    //  if we send a CRLF, the firewall thinks the CR is part of
    //  the username:
    success = CkSsh_ChannelSendString(ssh,channelNum,"myLogin\n","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 our firewall password.
    //  Terminate the password with a bare-LF
    success = CkSsh_ChannelSendString(ssh,channelNum,"myPassword\n","ansi");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        return;
    }

    //  Assuming it's successful, we should get a command prompt.
    //  In this case, it is "IS-SGxxx>"
    success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"IS-SGxxx>","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.  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 "help" command:
    success = CkSsh_ChannelSendString(ssh,channelNum,"help\n","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,"IS-SGxxx>","ansi",TRUE);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        printf("%s\n",CkSsh_sessionLog(ssh));
        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 empty 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.