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 -- 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".

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, '30-day trial'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    DECLARE @hostname nvarchar(4000)

    DECLARE @port int

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

    --  Keep a session log, which is available via the SessionLog
    --  property:
    EXEC sp_OASetProperty @ssh, 'KeepSessionLog', 1

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

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  When reading, if no additional data arrives for more than
    --  5 seconds, then abort:
    EXEC sp_OASetProperty @ssh, 'IdleTimeoutMs', 5000

    --  SSH Server Authentication
    --  If there is no login/password required, you must still call
    --  AuthenticatePw and use any values for login/password.
    EXEC sp_OAMethod @ssh, 'AuthenticatePw', @success OUT, 'chilkat', 'myPassword'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  Open a session channel.
    DECLARE @channelNum int

    EXEC sp_OAMethod @ssh, 'OpenSessionChannel', @channelNum OUT
    IF @channelNum < 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  Request a pseudo-terminal
    DECLARE @termType nvarchar(4000)

    SELECT @termType = 'dumb'
    DECLARE @widthInChars int

    SELECT @widthInChars = 120
    DECLARE @heightInChars int

    SELECT @heightInChars = 40
    DECLARE @pixWidth int

    SELECT @pixWidth = 0
    DECLARE @pixHeight int

    SELECT @pixHeight = 0
    EXEC sp_OAMethod @ssh, 'SendReqPty', @success OUT, @channelNum, @termType, @widthInChars, @heightInChars, @pixWidth, @pixHeight
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  Start a shell on the channel:
    EXEC sp_OAMethod @ssh, 'SendReqShell', @success OUT, @channelNum
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  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.)
    DECLARE @cmd nvarchar(4000)

ERROR-CONCAT    SELECT @cmd = 'su' + CHAR(10)
    EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, @cmd, 'ansi'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  Read until we get the prompt for the password:
    EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, 'Password:', 'ansi', 1
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  Display what we've received so far:
    EXEC sp_OAMethod @ssh, 'GetReceivedText', @sTmp0 OUT, @channelNum, 'ansi'
    PRINT @sTmp0

    --  Send the password.
    --  Again, make sure it uses a bare-LF and not a CRLF.
    DECLARE @password nvarchar(4000)

ERROR-CONCAT    SELECT @password = 'myPassword' + CHAR(10)
    EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, @password, 'ansi'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  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.
    EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, 'root@ubuntu:/home/chilkat#', 'ansi', 1
    IF @success <> 1
      BEGIN
        --  Check the last-error information and the session log...
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        --  Check to see what was received.
        EXEC sp_OAMethod @ssh, 'GetReceivedText', @sTmp0 OUT, @channelNum, 'ansi'
        PRINT @sTmp0
        RETURN
      END

    --  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...
    EXEC sp_OAMethod @ssh, 'GetReceivedText', @sTmp0 OUT, @channelNum, 'ansi'
    PRINT @sTmp0

    --  Send a command.  In this case, we are sending the "ls" command:
ERROR-CONCAT    SELECT @cmd = 'ls' + CHAR(10)
    EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, @cmd, 'ansi'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

    --  Read until the next command prompt:
    EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, 'root@ubuntu:/home/chilkat#', 'ansi', 1
    IF @success <> 1
      BEGIN
        --  Check the last-error information and the session log...
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        --  Check to see what was received.
        EXEC sp_OAMethod @ssh, 'GetReceivedText', @sTmp0 OUT, @channelNum, 'ansi'
        PRINT @sTmp0
        RETURN
      END

    --  Display the command output:
    EXEC sp_OAMethod @ssh, 'GetReceivedText', @sTmp0 OUT, @channelNum, 'ansi'
    PRINT @sTmp0

    --  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.
    EXEC sp_OAMethod @ssh, 'ChannelSendEof', @success OUT, @channelNum
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

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

        PRINT @sTmp0
        EXEC sp_OAGetProperty @ssh, 'SessionLog', @sTmp0 OUT

        PRINT @sTmp0
        RETURN
      END

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

 

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