SQL Server
SQL Server
SSH Quick/Simple Shell Session
See more SSH Examples
Demonstrates the simplified way to run multiple commands in a shell session using QuickShell, which combines OpenSessionChannel, SendReqPty, and SendReqShell into one call. The commands are built in a StringBuilder, sent together, and the output is read up to a marker echoed by the final command.
Background: Note that
QuickShell does allocate a PTY, so the remote shell runs interactively: it prints a prompt and echoes back every command sent to it, and both the echo and the real output appear in what is received. That is what motivates the quoting trick on the final marker — writing echo THIS 'IS' THE END means the echoed command line contains the quotes while the command's actual output does not, so matching the unquoted text reliably matches the real output instead of the echo. For clean, prompt-free output, request a shell without a PTY instead.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Demonstrates the simplified way to run multiple commands in a shell session using QuickShell.
--
-- Note: QuickShell internally does OpenSessionChannel, SendReqPty, and SendReqShell, so it DOES
-- allocate a pseudo-terminal. The remote shell therefore runs interactively: it prints a
-- command prompt and echoes back the commands that are sent to it. Both the echoed input and
-- the real output appear in what is received.
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @port int
SELECT @port = 22
EXEC sp_OAMethod @ssh, 'Connect', @success OUT, 'ssh.example.com', @port
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- 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.
DECLARE @password nvarchar(4000)
SELECT @password = 'mySshPassword'
EXEC sp_OAMethod @ssh, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- Start the shell session. A negative return value indicates failure.
DECLARE @channelNum int
EXEC sp_OAMethod @ssh, 'QuickShell', @channelNum OUT
IF @channelNum < 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- Build the commands, one per line. Line endings matter: Unix/Linux servers typically expect
-- a bare LF, while Windows servers may require CRLF.
DECLARE @sbCommands int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbCommands OUT
EXEC sp_OAMethod @sbCommands, 'Append', @success OUT, 'echo hello world' + CHAR(10)
EXEC sp_OAMethod @sbCommands, 'Append', @success OUT, 'date' + CHAR(10)
EXEC sp_OAMethod @sbCommands, 'Append', @success OUT, 'df' + CHAR(10)
-- The final command echoes a marker used to detect the end of the output. The single quotes
-- around 'IS' are a trick: the terminal echo of the typed command includes the quotes, while
-- the command's actual output does not. Matching the unquoted form therefore matches the real
-- output rather than the echo.
EXEC sp_OAMethod @sbCommands, 'Append', @success OUT, 'echo THIS ''IS'' THE END OF THE SCRIPT' + CHAR(10)
DECLARE @commands nvarchar(4000)
EXEC sp_OAMethod @sbCommands, 'GetAsString', @commands OUT
EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, @commands, 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
RETURN
END
-- 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.
EXEC sp_OASetProperty @ssh, 'ReadTimeoutMs', 15000
DECLARE @caseSensitive int
SELECT @caseSensitive = 1
EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, 'THIS IS THE END OF THE SCRIPT', 'utf-8', @caseSensitive
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
RETURN
END
-- Indicate that no more commands will be sent, then close the channel. Close only after the
-- desired output has been received.
EXEC sp_OAMethod @ssh, 'ChannelSendEof', @success OUT, @channelNum
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
RETURN
END
EXEC sp_OAMethod @ssh, 'ChannelSendClose', @success OUT, @channelNum
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
RETURN
END
-- Collect any remaining output.
EXEC sp_OAMethod @ssh, 'ChannelReceiveToClose', @success OUT, @channelNum
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
RETURN
END
DECLARE @sessionOutput nvarchar(4000)
EXEC sp_OAMethod @ssh, 'GetReceivedText', @sessionOutput OUT, @channelNum, 'utf-8'
EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
RETURN
END
PRINT '--- output ----'
PRINT @sessionOutput
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sbCommands
END
GO