SQL Server
SQL Server
Receive from an SSH Channel Until Any Pattern Matches
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelReceiveUntilMatchN method, which works like ChannelReceiveUntilMatch but returns when any pattern in a StringArray matches. The arguments are the channel number, the pattern array, the charset, and case sensitivity. The array must contain at least one element.
Important: Set the ReadTimeoutMs property before calling this method. It defaults to 0, meaning no limit, so without it the call waits forever if the received data never contains a match.
Background: A command often has more than one possible outcome, and your code should react to whichever occurs rather than hang waiting for one specific string. A common non-interactive pattern is to have the command print one marker on success and a different marker on failure, then match on the set. (With no PTY there is no shell prompt to match against, so markers you emit yourself are the dependable signal.) The first matching content in the stream wins; array order does not override an earlier token. Empty elements are ignored and duplicates are allowed.
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.ChannelReceiveUntilMatchN method, which receives text until ANY pattern
-- in a StringArray is matched. The 1st argument is the channel number, the 2nd is the array of
-- patterns, the 3rd is the charset, and the 4th selects case-sensitive matching.
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
-- Start a shell WITHOUT requesting a PTY. Without a PTY the shell sees it is connected to a
-- raw data pipe and enters non-interactive mode: it suppresses the command prompt, does not
-- echo typed commands back, and skips interactive login scripts. The output is therefore
-- clean and easily parseable -- but because no prompt is ever sent, code must not wait for
-- one. Instead, have each command print a predictable marker, or wait for EOF / channel close.
EXEC sp_OAMethod @ssh, 'SendReqShell', @success OUT, @channelNum
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- There is no prompt to match on. Instead, run a command that prints one marker on success
-- and a different marker on failure, then wait for whichever appears first.
EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, 'test -f /etc/hosts && echo OK_MARKER || echo ERR_MARKER' + CHAR(10), 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
DECLARE @patterns int
EXEC @hr = sp_OACreate 'Chilkat.StringArray', @patterns OUT
EXEC sp_OAMethod @patterns, 'Append', @success OUT, 'OK_MARKER'
EXEC sp_OAMethod @patterns, 'Append', @success OUT, 'ERR_MARKER'
-- 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 = 0
EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatchN', @success OUT, @channelNum, @patterns, '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 @patterns
RETURN
END
DECLARE @output nvarchar(4000)
EXEC sp_OAMethod @ssh, 'GetReceivedText', @output 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 @patterns
RETURN
END
PRINT @output
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @patterns
END
GO