SQL Server
SQL Server
Get Received SSH Text into a StringBuilder
See more SSH Examples
Demonstrates the Chilkat Ssh.GetReceivedSb method, which decodes a channel's receive buffer and appends the text to a StringBuilder. The arguments are the channel number, the charset, and the StringBuilder. The receive buffer is cleared afterward.
Background: Because this appends rather than replaces, it is the natural choice for accumulating output across repeated reads — call it in a loop and the
StringBuilder grows into the complete transcript. It also avoids creating a new string on every retrieval, which matters when a command produces a large amount of text.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
-- Demonstrates the Ssh.GetReceivedSb method, which decodes the channel's receive buffer and
-- appends the text to a StringBuilder. The 1st argument is the channel number, the 2nd is the
-- charset, and the 3rd is the StringBuilder. The receive buffer is cleared afterward.
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @sshPort int
SELECT @sshPort = 22
EXEC sp_OAMethod @ssh, 'Connect', @success OUT, 'ssh.example.com', @sshPort
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
DECLARE @channelNum int
EXEC sp_OAMethod @ssh, 'OpenSessionChannel', @channelNum OUT
IF @channelNum < 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
EXEC sp_OAMethod @ssh, 'SendReqExec', @success OUT, @channelNum, 'ls -l /tmp'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- IMPORTANT: Set a read timeout. ReadTimeoutMs defaults to 0, which means no limit -- without
-- it, this call waits forever if the server never sends channel close.
EXEC sp_OASetProperty @ssh, 'ReadTimeoutMs', 15000
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
RETURN
END
-- Append the received text to a StringBuilder. Existing content is preserved.
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @ssh, 'GetReceivedSb', @success OUT, @channelNum, 'utf-8', @sb
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sb
RETURN
END
EXEC sp_OAGetProperty @sb, 'Length', @iTmp0 OUT
PRINT 'Characters received: ' + @iTmp0
DECLARE @output nvarchar(4000)
EXEC sp_OAMethod @sb, 'GetAsString', @output OUT
PRINT @output
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @sb
END
GO