Sample code for 30+ languages & platforms
AutoIt

Check Whether an SSH Channel is Open

See more SSH Examples

Demonstrates the Chilkat Ssh.ChannelIsOpen method, which reports whether a channel number identifies a channel Chilkat currently considers open for application I/O. The only argument is the channel number. This example checks the channel before and after sending CLOSE.

Background: Because one SSH connection can carry many channels with independent lifetimes, code that manages several needs a way to ask whether a given one is still usable. This returns false for invalid or nonexistent channel numbers, after a local Disconnect, once remote closure is processed, and immediately after ChannelSendClose — which marks the channel locally closed without waiting for the remote side.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the Ssh.ChannelIsOpen method, which reports whether a channel number identifies
;  a channel Chilkat currently considers open for application I/O.  The only argument is the
;  channel number.

$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

;  Open a session channel.  A negative return value indicates failure.
Local $iChannelNum = $oSsh.OpenSessionChannel()
If ($iChannelNum < 0) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

If ($oSsh.ChannelIsOpen($iChannelNum)) Then
    ConsoleWrite("The channel is open." & @CRLF)
Else
    ConsoleWrite("The channel is not open." & @CRLF)
EndIf

;  Send CLOSE, which immediately marks the channel locally closed.
$bSuccess = $oSsh.ChannelSendClose($iChannelNum)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

If ($oSsh.ChannelIsOpen($iChannelNum)) Then
    ConsoleWrite("Still open." & @CRLF)
Else
    ConsoleWrite("The channel is now closed." & @CRLF)
EndIf

$oSsh.Disconnect