AutoIt
AutoIt
Send EOF on an SSH Channel
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelSendEof method, which sends an SSH EOF message on a channel. The only argument is the channel number. EOF closes only the client-to-server direction; the application may still receive stdout, stderr, exit status, EOF, and CLOSE from the server.
Background: Many remote commands read standard input until it ends —
wc, sort, cat and friends. Sending EOF is the SSH equivalent of pressing Ctrl+D: it tells the remote process no more input is coming so it can finish and emit results. After sending EOF, do not send more data on that channel; a later send may return true even though nothing is delivered.Chilkat AutoIt Downloads
Local $bSuccess = False
; Demonstrates the Ssh.ChannelSendEof method, which sends an SSH EOF message on a channel. The
; only argument is the channel number. EOF closes only the client-to-server direction; output
; may still be received from the server.
$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,"wc -l")
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
; Send input to the remote command.
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"line one" & @LF & "line two" & @LF & "line three" & @LF,"utf-8")
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
; Signal that no more input will be sent. Do not send more data on this channel afterward.
$bSuccess = $oSsh.ChannelSendEof($iChannelNum)
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
; Output can still be received after EOF is sent.
$bSuccess = $oSsh.ChannelReceiveToClose($iChannelNum)
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
Local $sOutput = $oSsh.GetReceivedText($iChannelNum,"utf-8")
If ($oSsh.LastMethodSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite($sOutput & @CRLF)
$oSsh.Disconnect