Sample code for 30+ languages & platforms
DataFlex

Send CLOSE on an SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.ChannelSendClose method, which sends an SSH CLOSE message for a channel and immediately marks it locally closed. The only argument is the channel number. It does not wait for remote EOF, remote CLOSE, or an exit status.

Background: CLOSE shuts down both directions of the channel, unlike EOF which closes only your side. Because the call returns without waiting for the remote side to acknowledge, it does not guarantee the remote process has terminated — so retrieve any output and exit status you care about before calling it. Afterward ChannelIsOpen reports false, though NumOpenChannels may still count the channel until final remote-close processing.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannelNum
    String sOutput
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the Ssh.ChannelSendClose method, which sends an SSH CLOSE message for a channel
    //  and immediately marks it locally closed.  The only argument is the channel number.  It does
    //  not wait for remote EOF, remote CLOSE, or an exit status.

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    Move 22 To iSshPort
    Get ComConnect Of hoSsh "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComOpenSessionChannel Of hoSsh To iChannelNum
    If (iChannelNum < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSendReqExec Of hoSsh iChannelNum "uptime" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  IMPORTANT: Set a read timeout.  ReadTimeoutMs defaults to 0, which means no limit -- without
    //  it, this call waits forever if the server never sends channel close.
    Set ComReadTimeoutMs Of hoSsh To 15000

    Get ComChannelReceiveToClose Of hoSsh iChannelNum To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sOutput
    Get ComLastMethodSuccess Of hoSsh To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sOutput

    //  Send CLOSE for the channel.
    Get ComChannelSendClose Of hoSsh iChannelNum To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  After a successful call, the channel is no longer reported as open.
    Get ComChannelIsOpen Of hoSsh iChannelNum To bTemp1
    If (bTemp1) Begin
        Showln "Still open."
    End
    Else Begin
        Showln "The channel is closed."
    End

    Send ComDisconnect To hoSsh


End_Procedure