PowerBuilder
PowerBuilder
Get Received SSH Text into a StringBuilder
See more SSH Examples
Demonstrates the Chilkat Ssh.GetReceivedSb method, which decodes a channel's receive buffer and appends the text to a StringBuilder. The arguments are the channel number, the charset, and the StringBuilder. The receive buffer is cleared afterward.
Background: Because this appends rather than replaces, it is the natural choice for accumulating output across repeated reads — call it in a loop and the
StringBuilder grows into the complete transcript. It also avoids creating a new string on every retrieval, which matters when a command produces a large amount of text.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
oleobject loo_Sb
string ls_Output
li_Success = 0
// Demonstrates the Ssh.GetReceivedSb method, which decodes the channel's receive buffer and
// appends the text to a StringBuilder. The 1st argument is the channel number, the 2nd is the
// charset, and the 3rd is the StringBuilder. The 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
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
// Append the received text to a StringBuilder. Existing content is preserved.
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Ssh.GetReceivedSb(li_ChannelNum,"utf-8",loo_Sb)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_Sb
return
end if
Write-Debug "Characters received: " + string(loo_Sb.Length)
ls_Output = loo_Sb.GetAsString()
Write-Debug ls_Output
loo_Ssh.Disconnect()
destroy loo_Ssh
destroy loo_Sb