Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set ssh [new_CkSsh]

#  Connect and authenticate a standalone SSH object.
set success [CkSsh_Connect $ssh "ssh.example.com" 22]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  The SSH password should come from a secure source rather than being hard-coded.
set sshPassword "mySshPassword"
set success [CkSsh_AuthenticatePw $ssh "sshUser" $sshPassword]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

set socket [new_CkSocket]

#  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.
set success [CkSocket_UseSsh $socket $ssh]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSsh $ssh
    delete_CkSocket $socket
    exit
}

#  Connect to the destination through the SSH tunnel.
set bTls 0
set maxWaitMs 5000
set success [CkSocket_Connect $socket "db.internal" 5432 $bTls $maxWaitMs]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSsh $ssh
    delete_CkSocket $socket
    exit
}

puts "Connected to the destination through the SSH tunnel."

delete_CkSsh $ssh
delete_CkSocket $socket