SQL Server
SQL Server
SSH Parallel Remote Commands on a Single Server
See more SSH Examples
Demonstrates running several commands in parallel on one SSH server and collecting each command's output as it finishes. QuickCmdSend starts each command and returns immediately; QuickCmdCheck reports them as they complete.
Background: Because SSH multiplexes channels, several commands can run at once over one authenticated connection — far faster than running them one after another when each involves waiting. Results arrive in completion order rather than the order the commands were started, so the loop keys off the returned channel number. Distinguish the two negative returns carefully:
-1 means "still working, ask again," while -2 means nothing remains to collect or the connection failed.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 running several commands in parallel on a single SSH server and collecting each
-- command's output as it finishes.
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
-- QuickCmdSend starts a command and returns immediately, so all three run concurrently, each on
-- its own session channel.
DECLARE @channel1 int
EXEC sp_OAMethod @ssh, 'QuickCmdSend', @channel1 OUT, 'df'
IF @channel1 < 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
DECLARE @channel2 int
EXEC sp_OAMethod @ssh, 'QuickCmdSend', @channel2 OUT, 'date'
IF @channel2 < 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
DECLARE @channel3 int
EXEC sp_OAMethod @ssh, 'QuickCmdSend', @channel3 OUT, 'echo hello world'
IF @channel3 < 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- Collect the results. QuickCmdCheck returns the channel number of a completed command,
-- -1 when commands are still pending but none finished within the timeout, or -2 when nothing
-- remains to be checked (or an error occurred).
DECLARE @pollTimeoutMs int
SELECT @pollTimeoutMs = 50
DECLARE @numFinished int
SELECT @numFinished = 0
WHILE @numFinished < 3
BEGIN
DECLARE @channel int
EXEC sp_OAMethod @ssh, 'QuickCmdCheck', @channel OUT, @pollTimeoutMs
IF @channel = -2
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
IF @channel >= 0
BEGIN
PRINT '---- channel ' + @channel + ' finished ----'
DECLARE @cmdOutput nvarchar(4000)
EXEC sp_OAMethod @ssh, 'GetReceivedText', @cmdOutput OUT, @channel, '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
RETURN
END
PRINT @cmdOutput
SELECT @numFinished = @numFinished + 1
END
END
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
END
GO