SQL Server Stored Procedure Examples

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

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
DKIM / DomainKey
FTP
HTML Conversion
HTTP
MHT
MIME
NTLM
RSA
Diffie-Hellman
DSA
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
String
Tar
Upload
XML
XMP
Zip

Amazon S3
Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

SSH Tunnel (Port Forwarding via direct-tcpip channel)

Demonstrates how to create an SSH tunnel to a remote hostname:port via a direct-tcpip channel.

Download Chilkat 32-bit SSH / SFTP ActiveX (.msi)

Download All 32-bit Chilkat ActiveX Components (.zip)

Download All 64-bit Chilkat ActiveX Components (.zip)

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  Important: It is helpful to send the contents of the
    --  ssh.LastErrorText property when requesting support.

    DECLARE @ssh int
    EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Any string automatically begins a fully-functional 30-day trial.
    DECLARE @success int

    EXEC sp_OAMethod @ssh, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Connect to an SSH server:
    DECLARE @hostname nvarchar(4000)

    DECLARE @port int

    --  Hostname may be an IP address or hostname:
    SELECT @hostname = '192.168.1.117'
    SELECT @port = 22

    EXEC sp_OAMethod @ssh, 'Connect', @success OUT, @hostname, @port
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Wait a max of 5 seconds when reading responses..
    EXEC sp_OASetProperty @ssh, 'IdleTimeoutMs', 5000

    --  Authenticate using login/password:
    EXEC sp_OAMethod @ssh, 'AuthenticatePw', @success OUT, 'chilkat', 'myPassword'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Open a direct-tcpip channel.  We want the SSH server to connect
    --  to www.chilkatsoft.com, port 80 (i.e. the web server).
    --  Data sent through the SSH tunnel is forwarded to the remote
    --  host:port.  (Note: The remote host:port does not need to be
    --  a web server.  It can be anything.  It can be your own
    --  customer application server that listens on a port, or any
    --  other type of server.)
    --  When we read from the SSH channel, we'll be reading data
    --  sent from the remote host:port (i.e. the web server in this
    --  example).
    DECLARE @channelNum int

    EXEC sp_OAMethod @ssh, 'OpenDirectTcpIpChannel', @channelNum OUT, 'www.chilkatsoft.com', 80
    IF @channelNum < 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Build a simple HTTP GET request for http://www.chilkatsoft.com/xyz.html
    DECLARE @httpReq nvarchar(4000)

    SELECT @httpReq = 'GET /xyz123.html HTTP/1.1\r\nHost: www.chilkatsoft.com\r\n\r\n'

    --  Send the HTTP request:
    EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, @httpReq, 'ansi'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Get the HTTP response.
    --  First read the HTTP response header which ends with a double CRLF.
    --  Calling ChannelReceiveUntilMatch will receive until match string is seen,
    --  or until a timeout occurs (IdleTimeoutMs property).  ChannelReceiveUntilMatch
    --  may read beyond the match string, but it will stop reading as soon as the match
    --  string is seen.
    DECLARE @caseSensitive int

    SELECT @caseSensitive = 0
    DECLARE @matchStr nvarchar(4000)

    SELECT @matchStr = '\r\n\r\n'
    EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, @matchStr, 'ansi', @caseSensitive
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Extract the HTTP header from the receive buffer.
    --  (GetReceiveTextS extracts up to and including the match string from the receive buffer)
    DECLARE @responseHeader nvarchar(4000)

    EXEC sp_OAMethod @ssh, 'GetReceivedTextS', @responseHeader OUT, @channelNum, @matchStr, 'ansi'

    PRINT '---- HTTP Response Header ----'

    PRINT @responseHeader

    --  Now get the body of the HTTP response (this is the HTML content
    --  of http://www.chilkatsoft.com/xyz.html
    --  It's possible we've already received the entire HTTP response in the
    --  call to ChannelReceiveUntilMatch.  Therefore, we'll poll for any remaining data
    --  and wait a max of .2 seconds.
    DECLARE @numBytesRead int

    DECLARE @pollTimeoutMs int

    SELECT @pollTimeoutMs = 200
    EXEC sp_OAMethod @ssh, 'ChannelPoll', @numBytesRead OUT, @channelNum, @pollTimeoutMs
    --  We're not checking for an error here.
    --  A return value of -2 means that no data was available and the poll simply timed out (not an error)
    --  A return value of -1 indicates an error.
    --  A return value greater than 0 indicates that additional data was received.


    PRINT '---- HTML BODY ----'

    --  Extract the remainder of the accumulated data in the internal receive buffer.
    --  This should be our HTML body:
    DECLARE @htmlBody nvarchar(4000)

    EXEC sp_OAMethod @ssh, 'GetReceivedText', @htmlBody OUT, @channelNum, 'ansi'

    PRINT @htmlBody

    --  Close the channel:
    EXEC sp_OAMethod @ssh, 'ChannelSendClose', @success OUT, @channelNum
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Disconnect
    EXEC sp_OAMethod @ssh, 'Disconnect', NULL
END
GO

 

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