SQL Server
SQL Server
SSH Commands to a Cisco Switch
See more SSH Examples
Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending commands in a device console session. Each command is sent, and its output is read up to the device's next prompt.
Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.
Background: Network devices differ from general-purpose servers in that they expose a menu- or prompt-driven console rather than a Unix shell, so
exec requests are usually unavailable and driving the interactive console is the only option. That makes prompt matching the synchronization mechanism, and it is why a PTY is appropriate here despite the general advice to avoid one. Retrieving the received text between commands is essential: a prompt left in the buffer would make the next receive return immediately with the wrong output.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 establishing an SSH session with a Cisco switch (or similar device) and sending
-- commands in a device console session.
--
-- Note: QuickShell allocates a PTY, which is what a network device console expects. The device
-- presents an interactive prompt, and each command's output is read up to the next prompt.
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, '172.16.16.100', @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 a shell session.
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
-- 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
-- The switch presents a prompt ending in "#". Reading up to it confirms the session is ready.
EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, '#', 'utf-8', @caseSensitive
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
DECLARE @cmdOutput nvarchar(4000)
EXEC sp_OAMethod @ssh, 'GetReceivedText', @cmdOutput 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
RETURN
END
PRINT @cmdOutput
-- Send a command and read its output up to the next prompt.
EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, 'show clock' + CHAR(10), 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, '#', 'utf-8', @caseSensitive
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- Retrieving the text also clears the receive buffer, which matters: if the prompt were left
-- buffered, the next receive would match it immediately and return the wrong output.
EXEC sp_OAMethod @ssh, 'GetReceivedText', @cmdOutput 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
RETURN
END
PRINT @cmdOutput
-- Additional commands follow the same send / read-to-prompt / retrieve pattern.
EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, 'show version' + CHAR(10), 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, '#', 'utf-8', @caseSensitive
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
EXEC sp_OAMethod @ssh, 'GetReceivedText', @cmdOutput 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
RETURN
END
PRINT @cmdOutput
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
END
GO