Visual Basic 6.0
Visual Basic 6.0
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 Visual Basic 6.0 Downloads
Dim success As Long
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.
Dim ssh As New ChilkatSsh
Dim sshPort As Long
sshPort = 22
success = ssh.Connect("ssh.example.com",sshPort)
If (success = 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
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 = 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
End If
' Keep stderr in its own buffer rather than merging it into the normal receive buffer.
ssh.StderrToStdout = 0
Dim channelNum As Long
channelNum = ssh.OpenSessionChannel()
If (channelNum < 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
End If
' Run a command that writes to both stdout and stderr.
success = ssh.SendReqExec(channelNum,"ls /etc/hosts /no/such/path")
If (success = 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
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 = 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
End If
Dim stdoutText As String
stdoutText = ssh.GetReceivedText(channelNum,"utf-8")
If (ssh.LastMethodSuccess = 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
End If
Debug.Print "STDOUT: " & stdoutText
Dim stderrText As String
stderrText = ssh.GetReceivedStderrText(channelNum,"utf-8")
If (ssh.LastMethodSuccess = 0) Then
Debug.Print ssh.LastErrorText
Exit Sub
End If
Debug.Print "STDERR: " & stderrText
ssh.Disconnect