Tcl
Tcl
Connect an SSH Tunnel Through a Jump Host
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.ConnectThroughSsh method, which connects to an SSH server through an already connected and authenticated Ssh object. The first argument is the jump-host Ssh object, and the second and third are the destination SSH hostname and port.
Background: This chains the tunnel through a "jump host" (bastion): the application reaches a gateway server, then tunnels onward to an SSH server that is only accessible from inside the network. Each hop authenticates separately with its own credentials. The result is a normal
SshTunnel — still requiring its own authentication and BeginAccepting — whose transport is routed through the first connection.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Demonstrates the SshTunnel.ConnectThroughSsh method, which connects to an SSH server through
# an already connected and authenticated Ssh object (a jump host). The 1st argument is the Ssh
# object, and the 2nd and 3rd are the destination SSH hostname and port.
set sshJump [new_CkSsh]
# Connect and authenticate to the jump host first.
set sshPort 22
set success [CkSsh_Connect $sshJump "jump.example.com" $sshPort]
if {$success == 0} then {
puts [CkSsh_lastErrorText $sshJump]
delete_CkSsh $sshJump
exit
}
# 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.
set password "mySshPassword"
set success [CkSsh_AuthenticatePw $sshJump "myJumpLogin" $password]
if {$success == 0} then {
puts [CkSsh_lastErrorText $sshJump]
delete_CkSsh $sshJump
exit
}
# Set up the tunnel and connect to the target SSH server through the jump host.
set tunnel [new_CkSshTunnel]
CkSshTunnel_put_DestHostname $tunnel "db.internal.example.com"
CkSshTunnel_put_DestPort $tunnel 5432
set success [CkSshTunnel_ConnectThroughSsh $tunnel $sshJump "ssh.internal.example.com" $sshPort]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSsh $sshJump
delete_CkSshTunnel $tunnel
exit
}
# Authenticate to the target SSH server (its own credentials).
set success [CkSshTunnel_AuthenticatePw $tunnel "mySshLogin" $password]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSsh $sshJump
delete_CkSshTunnel $tunnel
exit
}
# After authenticating, call BeginAccepting to start listening for local connections to forward.
set waitForThreadExit 1
set success [CkSshTunnel_CloseTunnel $tunnel $waitForThreadExit]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSsh $sshJump
delete_CkSshTunnel $tunnel
exit
}
CkSsh_Disconnect $sshJump
delete_CkSsh $sshJump
delete_CkSshTunnel $tunnel