Sample code for 30+ languages & platforms
PowerBuilder

Peek at Buffered SSH Channel Text

See more SSH Examples

Demonstrates the Chilkat Ssh.PeekReceivedText method, which returns the text currently buffered for a channel, decoded with the given charset, without removing any bytes. The first argument is the channel number and the second is the charset.

Background: Every other retrieval method consumes the buffer, which is awkward when you only want to know whether the output you are waiting for has arrived yet. Because peeking is nondestructive, you can inspect, decide, and still retrieve the data normally afterward — the basis of a poll-and-check loop. Note that StripColorCodes is not applied here, so terminal escape sequences appear exactly as buffered.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
string ls_Peeked
string ls_Output

li_Success = 0

//  Demonstrates the Ssh.PeekReceivedText method, which returns the text currently buffered for a
//  channel without removing any bytes.  The 1st argument is the channel number and the 2nd is
//  the charset.

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

li_Success = loo_Ssh.SendReqExec(li_ChannelNum,"ls -l /tmp")
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

//  Look at the buffered text without consuming it.  This is useful for checking whether the
//  expected output has arrived yet.
ls_Peeked = loo_Ssh.PeekReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "Peeked: " + ls_Peeked

//  The bytes are still buffered, so retrieving them still returns the same text.
ls_Output = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "Retrieved: " + ls_Output

loo_Ssh.Disconnect()


destroy loo_Ssh