Sample code for 30+ languages & platforms
DataFlex

Get Received SSH Bytes into a BinData

See more SSH Examples

Demonstrates the Chilkat Ssh.GetReceivedBd method, which appends the bytes currently in a channel's normal receive buffer to a BinData. The first argument is the channel number and the second is the BinData. The channel receive buffer is cleared afterward; existing bytes in the BinData are preserved.

Background: When the remote command emits binary — a tarball, an image, a compressed stream — decoding it as text through a charset would corrupt it. BinData keeps the bytes exactly as received, so you can write them to a file, hash them, or hand them to another API. Like the StringBuilder form it appends, making it easy to accumulate across successive reads. On failure the BinData is left unchanged.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannelNum
    Variant vBd
    Handle hoBd
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the Ssh.GetReceivedBd method, which appends the bytes currently in a channel's
    //  receive buffer to a BinData.  The 1st argument is the channel number and the 2nd is the
    //  BinData.  The channel receive buffer is cleared afterward.

    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

    //  Run a command whose output may be binary.
    Get ComSendReqExec Of hoSsh iChannelNum "cat /usr/bin/hostname" 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

    //  Append the raw received bytes to a BinData.  Existing bytes in the BinData are preserved.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get pvComObject of hoBd to vBd
    Get ComGetReceivedBd Of hoSsh iChannelNum vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBd To iTemp1
    Showln "Bytes received: " iTemp1

    Send ComDisconnect To hoSsh


End_Procedure