Sample code for 30+ languages & platforms
SQL Server

Check if the SSH Connection is Alive

See more SSH Examples

Demonstrates the Chilkat Ssh.CheckConnection method, which reports whether the underlying TCP socket still appears to be open. It takes no arguments and returns true if the socket does not appear closed, or false if the connection is known to be closed or invalid. This example connects, authenticates, checks the socket, and then uses SendIgnore for a definitive liveness test.

Background: Unlike the IsConnected property, which simply reports the state the Ssh object already knows, CheckConnection performs an additional non-consuming check of the socket — it calls the operating system's recv with MSG_PEEK to inspect up to one byte without removing any data from the receive buffer. It is still a local check: it sends no SSH message, does not authenticate, and does not prove the remote server is responsive. A silently broken network path can keep it returning true until the OS notices, and a receive timeout, aborted operation, auth failure, or rejected channel-open does not necessarily close the socket. When you need an actual SSH round trip, use SendIgnore. Also note a false result may simply mean no connection exists, and this method may not update LastErrorText, so an existing error string could describe an earlier operation.

Chilkat SQL Server Downloads

SQL Server
-- 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.CheckConnection method, which reports whether the underlying TCP socket
    --  still appears to be open.  It takes no arguments and returns 1 if the socket does not
    --  appear closed, or 0 if the connection is known to be closed or invalid.
    --  
    --  Unlike the IsConnected property, which reports the state already known to the Ssh object,
    --  CheckConnection performs an additional non-consuming peek at the socket.  It is still a local
    --  check: it does not send an SSH message or prove the remote server is responsive.  Use
    --  SendIgnore when an active SSH round trip is required.

    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

    --  Check whether the socket still appears open.
    EXEC sp_OAMethod @ssh, 'CheckConnection', @iTmp0 OUT
    IF @iTmp0
      BEGIN

        PRINT 'The socket still appears open.'
      END
    ELSE
      BEGIN

        PRINT 'The connection is closed or invalid.'
      END

    --  Note: a silently broken network path can still report open here until the OS detects it.
    --  For a definitive test that the SSH connection is writable, send an IGNORE message.
    EXEC sp_OAMethod @ssh, 'SendIgnore', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    PRINT 'SSH IGNORE sent -- the connection is writable.'

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh


END
GO