Sample code for 30+ languages & platforms
AutoIt

Route Socket Connections through an SSH Object

See more Socket/SSL/TLS Examples

Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.

Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oSsh = ObjCreate("Chilkat.Ssh")

;  Connect and authenticate a standalone SSH object.
$bSuccess = $oSsh.Connect("ssh.example.com",22)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

;  The SSH password should come from a secure source rather than being hard-coded.
Local $sshPassword = "mySshPassword"
$bSuccess = $oSsh.AuthenticatePw("sshUser",$sshPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

$oSocket = ObjCreate("Chilkat.Socket")

;  Route subsequent Connect calls through the already connected and authenticated SSH object.  The
;  destination is reached by SSH port forwarding rather than by a direct TCP connection.
$bSuccess = $oSocket.UseSsh($oSsh)
If ($bSuccess = False) Then
    ConsoleWrite($oSocket.LastErrorText & @CRLF)
    Exit
EndIf

;  Connect to the destination through the SSH tunnel.
Local $bTls = False
Local $iMaxWaitMs = 5000
$bSuccess = $oSocket.Connect("db.internal",5432,$bTls,$iMaxWaitMs)
If ($bSuccess = False) Then
    ConsoleWrite($oSocket.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Connected to the destination through the SSH tunnel." & @CRLF)