Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

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

$oSsh = ObjCreate("Chilkat.Ssh")

Local $iSshPort = 22
$bSuccess = $oSsh.Connect("ssh.example.com",$iSshPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

;  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.
Local $sPassword = "mySshPassword"

$bSuccess = $oSsh.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

Local $iChannelNum = $oSsh.OpenSessionChannel()
If ($iChannelNum < 0) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh.SendReqExec($iChannelNum,"ls -l /tmp")
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

;  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.
$oSsh.ReadTimeoutMs = 15000

$bSuccess = $oSsh.ChannelReceiveToClose($iChannelNum)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

;  Append the received text to a StringBuilder.  Existing content is preserved.
$oSb = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oSsh.GetReceivedSb($iChannelNum,"utf-8",$oSb)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Characters received: " & $oSb.Length & @CRLF)
Local $sOutput = $oSb.GetAsString()
ConsoleWrite($sOutput & @CRLF)

$oSsh.Disconnect