Sample code for 30+ languages & platforms
SQL Server

Clear Accumulated SSH TTY Modes

See more SSH Examples

Demonstrates the Chilkat Ssh.ClearTtyModes method, which clears all TTY modes previously added with SetTtyMode. It takes no arguments. The next SendReqPty will not include the cleared modes.

Background: Because TTY modes accumulate on the Ssh object, they would otherwise carry over into every later PTY request on that object. ClearTtyModes resets that state — useful when one Ssh object opens several channels and each needs different terminal settings, or when you want a PTY created with the server's defaults.

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 @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Ssh.ClearTtyModes method, which clears all TTY modes previously added with
    --  SetTtyMode.  It takes no arguments.

    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

    --  Add a TTY mode, then discard it before requesting the PTY.
    EXEC sp_OAMethod @ssh, 'SetTtyMode', @success OUT, 'ECHO', 0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Clear the accumulated TTY modes.  The next SendReqPty will not include them.
    EXEC sp_OAMethod @ssh, 'ClearTtyModes', NULL

    --  Open a session channel.  A negative return value indicates failure.
    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

    --  This PTY request carries no custom TTY modes.
    EXEC sp_OAMethod @ssh, 'SendReqPty', @success OUT, @channelNum, 'xterm', 80, 24, 0, 0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh


END
GO