Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
oleobject loo_Bd

li_Success = 0

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

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    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.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Run a command whose output may be binary.
li_Success = loo_Ssh.SendReqExec(li_ChannelNum,"cat /usr/bin/hostname")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    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.
loo_Ssh.ReadTimeoutMs = 15000

li_Success = loo_Ssh.ChannelReceiveToClose(li_ChannelNum)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Append the raw received bytes to a BinData.  Existing bytes in the BinData are preserved.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Ssh.GetReceivedBd(li_ChannelNum,loo_Bd)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_Bd
    return
end if

Write-Debug "Bytes received: " + string(loo_Bd.NumBytes)

loo_Ssh.Disconnect()


destroy loo_Ssh
destroy loo_Bd