Sample code for 30+ languages & platforms
AutoIt

Open an SSH Session Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.OpenSessionChannel method, which opens a new SSH session channel. It takes no arguments and returns the channel number, or -1 on failure. The transport must already be connected and authenticated.

Background: SSH multiplexes independent channels over one authenticated connection, and a session channel is the one used to run a command, start a shell, or invoke a subsystem. Opening the channel is only the container — nothing runs until you follow it with an exec, shell, or subsystem request. Always test for a negative return rather than assuming success.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the Ssh.OpenSessionChannel method, which opens a new SSH session channel.  It
;  takes no arguments and returns the channel number, or -1 on failure.  The transport must
;  already be connected and authenticated.

$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

ConsoleWrite("Opened session channel: " & $iChannelNum & @CRLF)

;  Opening the channel does not start a shell or command -- send an exec, shell, or subsystem
;  request next.

$oSsh.Disconnect