PowerBuilder
PowerBuilder
Get Text Received on the SSH stderr Buffer
See more SSH Examples
Demonstrates the Chilkat Ssh.GetReceivedStderrText method, which decodes and returns the text accumulated in a channel's separate stderr buffer. The first argument is the channel number and the second is the charset. The stderr buffer is cleared after the text is returned.
Background: This is only meaningful when
StderrToStdout is false, which keeps error output in its own buffer instead of merging it into the normal receive buffer. Separating the two lets a program parse a command's real output cleanly while still capturing diagnostics — important because many Unix tools write warnings to stderr that would otherwise corrupt the parsed result. With merging enabled, this buffer stays empty and the text appears via GetReceivedText instead.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
string ls_StdoutText
string ls_StderrText
li_Success = 0
// Demonstrates the Ssh.GetReceivedStderrText method, which decodes and returns the text in a
// channel's separate stderr buffer. The 1st argument is the channel number and the 2nd is the
// charset. The stderr buffer is cleared after the text is returned.
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
// Keep stderr in its own buffer rather than merging it into the normal receive buffer.
loo_Ssh.StderrToStdout = 0
li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Run a command that writes to both stdout and stderr.
li_Success = loo_Ssh.SendReqExec(li_ChannelNum,"ls /etc/hosts /no/such/path")
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
ls_StdoutText = 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 "STDOUT: " + ls_StdoutText
ls_StderrText = loo_Ssh.GetReceivedStderrText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug "STDERR: " + ls_StderrText
loo_Ssh.Disconnect()
destroy loo_Ssh