Sample code for 30+ languages & platforms
Xojo Plugin

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  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.

Dim ssh As New Chilkat.Ssh

Dim sshPort As Int32
sshPort = 22
success = ssh.Connect("ssh.example.com",sshPort)
If (success = False) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

//  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.
Dim password As String
password = "mySshPassword"

success = ssh.AuthenticatePw("mySshLogin",password)
If (success = False) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

Dim channelNum As Int32
channelNum = ssh.OpenSessionChannel()
If (channelNum < 0) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

//  Run a command whose output may be binary.
success = ssh.SendReqExec(channelNum,"cat /usr/bin/hostname")
If (success = False) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

//  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.
ssh.ReadTimeoutMs = 15000

success = ssh.ChannelReceiveToClose(channelNum)
If (success = False) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

//  Append the raw received bytes to a BinData.  Existing bytes in the BinData are preserved.
Dim bd As New Chilkat.BinData
success = ssh.GetReceivedBd(channelNum,bd)
If (success = False) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

System.DebugLog("Bytes received: " + Str(bd.NumBytes))

ssh.Disconnect