Sample code for 30+ languages & platforms
Tcl

Open a Forwarded Channel through an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshNewChannel, which opens a port-forwarded channel through an authenticated SSH tunnel to a destination host and port, populating a Socket with the connected channel.

Background. Set the ssl argument to true to establish TLS to the destination inside the SSH tunnel. The returned channel socket is then used to send and receive with the destination.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set socket [new_CkSocket]

#  Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
#  not required.
set success [CkSocket_SshOpenTunnel $socket "ssh.example.com" 22]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

#  Authenticate the SSH session before opening forwarded channels.
#  The SSH password should come from a secure source rather than being hard-coded.
set sshPassword "mySshPassword"
set success [CkSocket_SshAuthenticatePw $socket "sshUser" $sshPassword]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

#  Open a port-forwarded channel through the authenticated SSH tunnel to the destination host and
#  port, populating a Socket with the connected channel.  Set the 3rd argument to 1 to establish
#  TLS to the destination inside the tunnel.
set bTls 0
set maxWaitMs 20000
set channel [new_CkSocket]

set success [CkSocket_SshNewChannel $socket "db.internal" 5432 $bTls $maxWaitMs $channel]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    delete_CkSocket $channel
    exit
}

#  Use the channel socket to send and receive with the destination through the tunnel.
puts "Forwarded channel opened to the destination."

delete_CkSocket $socket
delete_CkSocket $channel